1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package jogamp.opengl.util.pngj.chunks;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.CRC32;
import jogamp.opengl.util.pngj.PngHelperInternal;
import jogamp.opengl.util.pngj.PngjBadCrcException;
import jogamp.opengl.util.pngj.PngjOutputException;
/**
* Raw (physical) chunk.
* <p>
* Short lived object, to be created while serialing/deserializing Do not reuse
* it for different chunks. <br>
* See http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html
*/
public class ChunkRaw {
/**
* The length counts only the data field, not itself, the chunk type code,
* or the CRC. Zero is a valid length. Although encoders and decoders should
* treat the length as unsigned, its value must not exceed 231-1 bytes.
*/
public final int len;
/**
* A 4-byte chunk type code. uppercase and lowercase ASCII letters
*/
public final byte[] idbytes = new byte[4];
/**
* The data bytes appropriate to the chunk type, if any. This field can be
* of zero length. Does not include crc
*/
public byte[] data = null;
/**
* A 4-byte CRC (Cyclic Redundancy Check) calculated on the preceding bytes
* in the chunk, including the chunk type code and chunk data fields, but
* not including the length field.
*/
private int crcval = 0;
/**
* @param len
* : data len
* @param idbytes
* : chunk type (deep copied)
* @param alloc
* : it true, the data array will be allocced
*/
public ChunkRaw(int len, byte[] idbytes, boolean alloc) {
this.len = len;
System.arraycopy(idbytes, 0, this.idbytes, 0, 4);
if (alloc)
allocData();
}
private void allocData() {
if (data == null || data.length < len)
data = new byte[len];
}
/**
* this is called after setting data, before writing to os
*/
private int computeCrc() {
CRC32 crcengine = PngHelperInternal.getCRC();
crcengine.reset();
crcengine.update(idbytes, 0, 4);
if (len > 0)
crcengine.update(data, 0, len); //
return (int) crcengine.getValue();
}
/**
* Computes the CRC and writes to the stream. If error, a
* PngjOutputException is thrown
*/
public void writeChunk(OutputStream os) {
if (idbytes.length != 4)
throw new PngjOutputException("bad chunkid [" + ChunkHelper.toString(idbytes) + "]");
crcval = computeCrc();
PngHelperInternal.writeInt4(os, len);
PngHelperInternal.writeBytes(os, idbytes);
if (len > 0)
PngHelperInternal.writeBytes(os, data, 0, len);
PngHelperInternal.writeInt4(os, crcval);
}
/**
* position before: just after chunk id. positon after: after crc Data
* should be already allocated. Checks CRC Return number of byte read.
*/
public int readChunkData(InputStream is, boolean checkCrc) {
PngHelperInternal.readBytes(is, data, 0, len);
crcval = PngHelperInternal.readInt4(is);
if (checkCrc) {
int crc = computeCrc();
if (crc != crcval)
throw new PngjBadCrcException("chunk: " + this + " crc calc=" + crc + " read=" + crcval);
}
return len + 4;
}
ByteArrayInputStream getAsByteStream() { // only the data
return new ByteArrayInputStream(data);
}
public String toString() {
return "chunkid=" + ChunkHelper.toString(idbytes) + " len=" + len;
}
}
|