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