001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.formats.png.chunks; 018 019import java.nio.charset.StandardCharsets; 020import java.util.logging.Level; 021import java.util.logging.Logger; 022 023import org.apache.commons.imaging.ImagingException; 024import org.apache.commons.imaging.common.BinaryFunctions; 025import org.apache.commons.imaging.formats.png.AbstractPngText; 026 027public final class PngChunkText extends AbstractPngTextChunk { 028 029 private static final Logger LOGGER = Logger.getLogger(PngChunkText.class.getName()); 030 031 private final String keyword; 032 private final String text; 033 034 /** 035 * Constructs a new instance. 036 * 037 * @param length chunk length. 038 * @param chunkType chunk type. 039 * @param crc CRC computed over the chunk type and chunk data (but not the length). 040 * @param bytes chunk data bytes. 041 * @throws ImagingException Thrown on a parsing error. 042 * @throws NullPointerException if {@code bytes} is null. 043 */ 044 public PngChunkText(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException { 045 super(length, chunkType, crc, bytes); 046 final int index = BinaryFunctions.indexOf0(bytes, "PNG tEXt chunk keyword is not terminated."); 047 keyword = new String(bytes, 0, index, StandardCharsets.ISO_8859_1); 048 final int textLength = bytes.length - (index + 1); 049 text = new String(bytes, index + 1, textLength, StandardCharsets.ISO_8859_1); 050 if (LOGGER.isLoggable(Level.FINEST)) { 051 LOGGER.finest("Keyword: " + keyword); 052 LOGGER.finest("Text: " + text); 053 } 054 } 055 056 @Override 057 public AbstractPngText getContents() { 058 return new AbstractPngText.Text(keyword, text); 059 } 060 061 /** 062 * @return the keyword. 063 */ 064 @Override 065 public String getKeyword() { 066 return keyword; 067 } 068 069 /** 070 * @return the text. 071 */ 072 @Override 073 public String getText() { 074 return text; 075 } 076 077}