View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 final class PngChunkItxt extends AbstractPngTextChunk {
32  
33      private final String keyword;
34      private final String text;
35  
36      /**
37       * The language tag defined in [RFC-3066] indicates the human language used by the translated keyword and the text. Unlike the keyword, the language tag is
38       * case-insensitive. It is an ISO 646.IRV:1991 [ISO 646] string consisting of hyphen-separated words of 1-8 alphanumeric characters each (for example cn,
39       * en-uk, no-bok, x-klingon, x-KlInGoN). If the first word is two or three letters long, it is an ISO language code [ISO-639]. If the language tag is empty,
40       * the language is unspecified.
41       */
42      private final String languageTag;
43  
44      private final String translatedKeyword;
45  
46      /**
47       * Constructs a new instance.
48       *
49       * @param length    chunk length.
50       * @param chunkType chunk type.
51       * @param crc       CRC computed over the chunk type and chunk data (but not the length).
52       * @param bytes     chunk data bytes.
53       * @throws ImagingException Thrown on a parsing error.
54       * @throws IOException Thrown on reading error.
55       * @throws NullPointerException if {@code bytes} is null.
56       */
57      public PngChunkItxt(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
58          super(length, chunkType, crc, bytes);
59          int terminator = BinaryFunctions.indexOf0(bytes, "PNG iTXt chunk keyword is not terminated.");
60  
61          keyword = new String(bytes, 0, terminator, StandardCharsets.ISO_8859_1);
62          int index = terminator + 1;
63  
64          final int compressionFlag = bytes[index++];
65          if (compressionFlag != 0 && compressionFlag != 1) {
66              throw new ImagingException("PNG iTXt chunk has invalid compression flag: " + compressionFlag);
67          }
68  
69          final boolean compressed = compressionFlag == 1;
70  
71          final int compressionMethod = bytes[index++];
72          if (compressed && compressionMethod != PngConstants.COMPRESSION_DEFLATE_INFLATE) {
73              throw new ImagingException("PNG iTXt chunk has unexpected compression method: " + compressionMethod);
74          }
75  
76          terminator = BinaryFunctions.indexOf0(bytes, index, "PNG iTXt chunk language tag is not terminated.");
77          languageTag = new String(bytes, index, terminator - index, StandardCharsets.ISO_8859_1);
78          index = terminator + 1;
79  
80          terminator = BinaryFunctions.indexOf0(bytes, index, "PNG iTXt chunk translated keyword is not terminated.");
81          translatedKeyword = new String(bytes, index, terminator - index, StandardCharsets.UTF_8);
82          index = terminator + 1;
83  
84          if (compressed) {
85              final int compressedTextLength = bytes.length - index;
86  
87              final byte[] compressedText = Allocator.byteArray(compressedTextLength);
88              System.arraycopy(bytes, index, compressedText, 0, compressedTextLength);
89  
90              text = new String(IOUtils.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedText))), StandardCharsets.UTF_8);
91  
92          } else {
93              text = new String(bytes, index, bytes.length - index, StandardCharsets.UTF_8);
94          }
95      }
96  
97      @Override
98      public AbstractPngText getContents() {
99          return new AbstractPngText.Itxt(keyword, text, languageTag, translatedKeyword);
100     }
101 
102     /**
103      * @return Gets the keyword.
104      */
105     @Override
106     public String getKeyword() {
107         return keyword;
108     }
109 
110     /**
111      * @return Gets the text.
112      */
113     @Override
114     public String getText() {
115         return text;
116     }
117 
118     public String getTranslatedKeyword() {
119         return translatedKeyword;
120     }
121 }