    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.tiff.taginfos;
    018
    019import java.nio.ByteOrder;
    020import java.nio.charset.StandardCharsets;
    021
    022import org.apache.commons.imaging.ImageReadException;
    023import org.apache.commons.imaging.ImageWriteException;
    024import org.apache.commons.imaging.formats.tiff.TiffField;
    025import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType;
    026import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType;
    027
    028/**
    029 * Windows XP onwards store some tags using UTF-16LE, but the field type is byte
    030 * - here we deal with this.
    031 */
    032public class TagInfoXpString extends TagInfo {
    033    public TagInfoXpString(final String name, final int tag, final TiffDirectoryType directoryType) {
    034        super(name, tag, FieldType.BYTE, LENGTH_UNKNOWN, directoryType);
    035    }
    036
    037    @Override
    038    public byte[] encodeValue(final FieldType fieldType, final Object value, final ByteOrder byteOrder)
    039            throws ImageWriteException {
    040        if (!(value instanceof String)) {
    041            throw new ImageWriteException("Text value not String", value);
    042        }
    043        final String s = (String) value;
    044        final byte[] bytes = s.getBytes(StandardCharsets.UTF_16LE);
    045        final byte[] paddedBytes = new byte[bytes.length + 2];
    046        System.arraycopy(bytes, 0, paddedBytes, 0, bytes.length);
    047        return paddedBytes;
    048    }
    049
    050    @Override
    051    public String getValue(final TiffField entry) throws ImageReadException {
    052        if (entry.getFieldType() != FieldType.BYTE) {
    053            throw new ImageReadException("Text field not encoded as bytes.");
    054        }
    055        final byte[] bytes = entry.getByteArrayValue();
    056        final int length;
    057        if (bytes.length >= 2 && bytes[bytes.length - 1] == 0 && bytes[bytes.length - 2] == 0) {
    058            length = bytes.length - 2;
    059        } else {
    060            length = bytes.length;
    061        }
    062        return new String(bytes, 0, length, StandardCharsets.UTF_16LE);
    063    }
    064}