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.nio.charset.StandardCharsets;
20  import java.util.logging.Level;
21  import java.util.logging.Logger;
22  
23  import org.apache.commons.imaging.ImagingException;
24  import org.apache.commons.imaging.common.BinaryFunctions;
25  import org.apache.commons.imaging.formats.png.AbstractPngText;
26  
27  public final class PngChunkText extends AbstractPngTextChunk {
28  
29      private static final Logger LOGGER = Logger.getLogger(PngChunkText.class.getName());
30  
31      private final String keyword;
32      private final String text;
33  
34      /**
35       * Constructs a new instance.
36       *
37       * @param length    chunk length.
38       * @param chunkType chunk type.
39       * @param crc       CRC computed over the chunk type and chunk data (but not the length).
40       * @param bytes     chunk data bytes.
41       * @throws ImagingException Thrown on a parsing error.
42       * @throws NullPointerException if {@code bytes} is null.
43       */
44      public PngChunkText(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException {
45          super(length, chunkType, crc, bytes);
46          final int index = BinaryFunctions.indexOf0(bytes, "PNG tEXt chunk keyword is not terminated.");
47          keyword = new String(bytes, 0, index, StandardCharsets.ISO_8859_1);
48          final int textLength = bytes.length - (index + 1);
49          text = new String(bytes, index + 1, textLength, StandardCharsets.ISO_8859_1);
50          if (LOGGER.isLoggable(Level.FINEST)) {
51              LOGGER.finest("Keyword: " + keyword);
52              LOGGER.finest("Text: " + text);
53          }
54      }
55  
56      @Override
57      public AbstractPngText getContents() {
58          return new AbstractPngText.Text(keyword, text);
59      }
60  
61      /**
62       * @return the keyword.
63       */
64      @Override
65      public String getKeyword() {
66          return keyword;
67      }
68  
69      /**
70       * @return the text.
71       */
72      @Override
73      public String getText() {
74          return text;
75      }
76  
77  }