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