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 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       * Gets the keyword.
62       *
63       * @return the keyword.
64       */
65      @Override
66      public String getKeyword() {
67          return keyword;
68      }
69  
70      /**
71       * Gets the text.
72       *
73       * @return the text.
74       */
75      @Override
76      public String getText() {
77          return text;
78      }
79  
80  }