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