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 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      public PngChunkItxt(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
47          super(length, chunkType, crc, bytes);
48          int terminator = BinaryFunctions.findNull(bytes, "PNG iTXt chunk keyword is not terminated.");
49  
50          keyword = new String(bytes, 0, terminator, StandardCharsets.ISO_8859_1);
51          int index = terminator + 1;
52  
53          final int compressionFlag = bytes[index++];
54          if (compressionFlag != 0 && compressionFlag != 1) {
55              throw new ImagingException("PNG iTXt chunk has invalid compression flag: " + compressionFlag);
56          }
57  
58          final boolean compressed = compressionFlag == 1;
59  
60          final int compressionMethod = bytes[index++];
61          if (compressed && compressionMethod != PngConstants.COMPRESSION_DEFLATE_INFLATE) {
62              throw new ImagingException("PNG iTXt chunk has unexpected compression method: " + compressionMethod);
63          }
64  
65          terminator = BinaryFunctions.findNull(bytes, index, "PNG iTXt chunk language tag is not terminated.");
66          languageTag = new String(bytes, index, terminator - index, StandardCharsets.ISO_8859_1);
67          index = terminator + 1;
68  
69          terminator = BinaryFunctions.findNull(bytes, index, "PNG iTXt chunk translated keyword is not terminated.");
70          translatedKeyword = new String(bytes, index, terminator - index, StandardCharsets.UTF_8);
71          index = terminator + 1;
72  
73          if (compressed) {
74              final int compressedTextLength = bytes.length - index;
75  
76              final byte[] compressedText = Allocator.byteArray(compressedTextLength);
77              System.arraycopy(bytes, index, compressedText, 0, compressedTextLength);
78  
79              text = new String(IOUtils.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedText))), StandardCharsets.UTF_8);
80  
81          } else {
82              text = new String(bytes, index, bytes.length - index, StandardCharsets.UTF_8);
83          }
84      }
85  
86      @Override
87      public AbstractPngText getContents() {
88          return new AbstractPngText.Itxt(keyword, text, languageTag, translatedKeyword);
89      }
90  
91      /**
92       * @return Gets the keyword.
93       */
94      @Override
95      public String getKeyword() {
96          return keyword;
97      }
98  
99      /**
100      * @return Gets the text.
101      */
102     @Override
103     public String getText() {
104         return text;
105     }
106 
107     public String getTranslatedKeyword() {
108         return translatedKeyword;
109     }
110 }