3 import java.util.zip.CRC32;
8 abstract class CRCEncoder extends ImageEncoder {
10 protected int startPos, bytePos;
13 protected byte[] pngBytes;
14 protected int dataLen;
15 private byte[] int2 = new byte[2];
16 private byte[] int4 = new byte[4];
19 pngBytes = new byte[250];
23 protected void setData(byte[] b, int pt) {
26 startPos = bytePos = pt;
29 protected byte[] getBytes() {
30 return (dataLen == pngBytes.length ? pngBytes : AU.arrayCopyByte(
34 protected void writeCRC() {
36 crc.update(pngBytes, startPos, bytePos - startPos);
37 writeInt4((int) crc.getValue());
41 * Write a two-byte integer into the pngBytes array at a given position.
43 * @param n The integer to be written into pngBytes.
45 protected void writeInt2(int n) {
46 int2[0] = (byte) ((n >> 8) & 0xff);
47 int2[1] = (byte) (n & 0xff);
52 * Write a four-byte integer into the pngBytes array at a given position.
54 * @param n The integer to be written into pngBytes.
56 protected void writeInt4(int n) {
61 protected static void getInt4(int n, byte[] int4) {
62 int4[0] = (byte) ((n >> 24) & 0xff);
63 int4[1] = (byte) ((n >> 16) & 0xff);
64 int4[2] = (byte) ((n >> 8) & 0xff);
65 int4[3] = (byte) (n & 0xff);
69 * Write a single byte into the pngBytes array at a given position.
71 * @param b The byte to be written into pngBytes.
73 protected void writeByte(int b) {
81 * Write a string into the pngBytes array at a given position.
82 * This uses the getBytes method, so the encoding used will
85 * @param s The string to be written into pngBytes.
86 * @see java.lang.String#getBytes()
88 protected void writeString(String s) {
89 writeBytes(s.getBytes());
93 * Write an array of bytes into the pngBytes array.
94 * Both dataLen and bytePos are updated. If we don't have
95 * enough room, this is certainly in image data writing,
96 * so we add just enough for CRC END CRC
99 * The data to be written into pngBytes.
101 protected void writeBytes(byte[] data) {
102 int newPos = bytePos + data.length;
103 dataLen = Math.max(dataLen, newPos);
104 if (newPos > pngBytes.length)
105 pngBytes = AU.arrayCopyByte(pngBytes, newPos + 16);
106 System.arraycopy(data, 0, pngBytes, bytePos, data.length);