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 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      public PngChunkText(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException {
35          super(length, chunkType, crc, bytes);
36          final int index = BinaryFunctions.findNull(bytes, "PNG tEXt chunk keyword is not terminated.");
37          keyword = new String(bytes, 0, index, StandardCharsets.ISO_8859_1);
38          final int textLength = bytes.length - (index + 1);
39          text = new String(bytes, index + 1, textLength, StandardCharsets.ISO_8859_1);
40          if (LOGGER.isLoggable(Level.FINEST)) {
41              LOGGER.finest("Keyword: " + keyword);
42              LOGGER.finest("Text: " + text);
43          }
44      }
45  
46      @Override
47      public AbstractPngText getContents() {
48          return new AbstractPngText.Text(keyword, text);
49      }
50  
51      /**
52       * @return Returns the keyword.
53       */
54      @Override
55      public String getKeyword() {
56          return keyword;
57      }
58  
59      /**
60       * @return Returns the text.
61       */
62      @Override
63      public String getText() {
64          return text;
65      }
66  
67  }