1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.png.chunks;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.nio.charset.StandardCharsets;
22 import java.util.zip.InflaterInputStream;
23
24 import org.apache.commons.imaging.ImagingException;
25 import org.apache.commons.imaging.common.Allocator;
26 import org.apache.commons.imaging.common.BinaryFunctions;
27 import org.apache.commons.imaging.formats.png.AbstractPngText;
28 import org.apache.commons.imaging.formats.png.PngConstants;
29 import org.apache.commons.io.IOUtils;
30
31 public class PngChunkZtxt extends AbstractPngTextChunk {
32
33 private final String keyword;
34 private final String text;
35
36 public PngChunkZtxt(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
37 super(length, chunkType, crc, bytes);
38
39 int index = BinaryFunctions.findNull(bytes, "PNG zTXt chunk keyword is unterminated.");
40 keyword = new String(bytes, 0, index, StandardCharsets.ISO_8859_1);
41 index++;
42
43 final int compressionMethod = bytes[index++];
44 if (compressionMethod != PngConstants.COMPRESSION_DEFLATE_INFLATE) {
45 throw new ImagingException("PNG zTXt chunk has unexpected compression method: " + compressionMethod);
46 }
47
48 final int compressedTextLength = bytes.length - index;
49 final byte[] compressedText = Allocator.byteArray(compressedTextLength);
50 System.arraycopy(bytes, index, compressedText, 0, compressedTextLength);
51
52 text = new String(IOUtils.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedText))), StandardCharsets.ISO_8859_1);
53 }
54
55 @Override
56 public AbstractPngText getContents() {
57 return new AbstractPngText.Ztxt(keyword, text);
58 }
59
60
61
62
63
64
65 @Override
66 public String getKeyword() {
67 return keyword;
68 }
69
70
71
72
73
74
75 @Override
76 public String getText() {
77 return text;
78 }
79
80 }