176 this.startPos = stream.getStreamPosition(); 177 stream.writeInt(-1); // length, will backpatch 178 179 crc.update(chunkType, 0, 4); 180 stream.write(chunkType, 0, 4); 181 182 this.bytesRemaining = chunkLength; 183 } 184 185 private void finishChunk() throws IOException { 186 // Write CRC 187 stream.writeInt(crc.getValue()); 188 189 // Write length 190 long pos = stream.getStreamPosition(); 191 stream.seek(startPos); 192 stream.writeInt((int)(pos - startPos) - 12); 193 194 // Return to end of chunk and flush to minimize buffering 195 stream.seek(pos); 196 stream.flushBefore(pos); 197 } 198 199 public int read() throws IOException { 200 throw new RuntimeException("Method not available"); 201 } 202 203 public int read(byte[] b, int off, int len) throws IOException { 204 throw new RuntimeException("Method not available"); 205 } 206 207 public void write(byte[] b, int off, int len) throws IOException { 208 if (len == 0) { 209 return; 210 } 211 212 if (!def.finished()) { 213 def.setInput(b, off, len); 214 while (!def.needsInput()) { 215 deflate(); 216 } | 176 this.startPos = stream.getStreamPosition(); 177 stream.writeInt(-1); // length, will backpatch 178 179 crc.update(chunkType, 0, 4); 180 stream.write(chunkType, 0, 4); 181 182 this.bytesRemaining = chunkLength; 183 } 184 185 private void finishChunk() throws IOException { 186 // Write CRC 187 stream.writeInt(crc.getValue()); 188 189 // Write length 190 long pos = stream.getStreamPosition(); 191 stream.seek(startPos); 192 stream.writeInt((int)(pos - startPos) - 12); 193 194 // Return to end of chunk and flush to minimize buffering 195 stream.seek(pos); 196 try { 197 stream.flushBefore(pos); 198 } catch (IOException e) { 199 /* 200 * If flushBefore() fails we try to access startPos in finally 201 * block of write_IDAT(). We should update startPos to avoid 202 * IndexOutOfBoundException while seek() is happening. 203 */ 204 this.startPos = stream.getStreamPosition(); 205 throw e; 206 } 207 } 208 209 public int read() throws IOException { 210 throw new RuntimeException("Method not available"); 211 } 212 213 public int read(byte[] b, int off, int len) throws IOException { 214 throw new RuntimeException("Method not available"); 215 } 216 217 public void write(byte[] b, int off, int len) throws IOException { 218 if (len == 0) { 219 return; 220 } 221 222 if (!def.finished()) { 223 def.setInput(b, off, len); 224 while (!def.needsInput()) { 225 deflate(); 226 } |