JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / img / CRCEncoder.java
1 package javajs.img;\r
2 \r
3 import java.util.zip.CRC32;\r
4 \r
5 \r
6 import javajs.util.AU;\r
7 \r
8 abstract class CRCEncoder extends ImageEncoder {\r
9 \r
10   protected int startPos, bytePos;\r
11   \r
12   private CRC32 crc;  \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
17 \r
18   CRCEncoder() {\r
19     pngBytes = new byte[250];\r
20     crc = new CRC32();\r
21   }\r
22 \r
23   protected void setData(byte[] b, int pt) {\r
24     pngBytes = b;\r
25     dataLen = b.length;\r
26     startPos = bytePos = pt;\r
27   }\r
28 \r
29   protected byte[] getBytes() {\r
30     return (dataLen == pngBytes.length ? pngBytes : AU.arrayCopyByte(\r
31         pngBytes, dataLen));\r
32   }\r
33 \r
34   protected void writeCRC() {\r
35     crc.reset();\r
36     crc.update(pngBytes, startPos, bytePos - startPos);\r
37     writeInt4((int) crc.getValue());\r
38   }\r
39 \r
40   /**\r
41    * Write a two-byte integer into the pngBytes array at a given position.\r
42    *\r
43    * @param n The integer to be written into pngBytes.\r
44    */\r
45   protected void writeInt2(int n) {\r
46     int2[0] = (byte) ((n >> 8) & 0xff);\r
47     int2[1] = (byte) (n & 0xff);\r
48     writeBytes(int2);\r
49   }\r
50 \r
51   /**\r
52    * Write a four-byte integer into the pngBytes array at a given position.\r
53    *\r
54    * @param n The integer to be written into pngBytes.\r
55    */\r
56   protected void writeInt4(int n) {\r
57     getInt4(n, int4);\r
58     writeBytes(int4);\r
59   }\r
60 \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
66   }\r
67 \r
68   /**\r
69    * Write a single byte into the pngBytes array at a given position.\r
70    *\r
71    * @param b The byte to be written into pngBytes.\r
72    */\r
73   protected void writeByte(int b) {\r
74     byte[] temp = {\r
75       (byte) b\r
76     };\r
77     writeBytes(temp);\r
78   }\r
79 \r
80   /**\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
83    * be its default.\r
84    *\r
85    * @param s The string to be written into pngBytes.\r
86    * @see java.lang.String#getBytes()\r
87    */\r
88   protected void writeString(String s) {\r
89     writeBytes(s.getBytes());\r
90   }\r
91 \r
92   /**\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
97    * \r
98    * @param data\r
99    *        The data to be written into pngBytes.\r
100    */\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
107     bytePos = newPos;\r
108   }\r
109 \r
110 }\r