Ignore:
Timestamp:
2017-10-30T22:46:09+01:00 (8 years ago)
Author:
Don-vip
Message:

fix #15505 - update to metadata-extractor 2.10.1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/drew/lang/RandomAccessReader.java

    r10862 r13061  
    11/*
    2  * Copyright 2002-2016 Drew Noakes
     2 * Copyright 2002-2017 Drew Noakes
    33 *
    44 *    Licensed under the Apache License, Version 2.0 (the "License");
     
    2222package com.drew.lang;
    2323
    24 import com.drew.lang.annotations.NotNull;
    25 
    2624import java.io.IOException;
    2725import java.io.UnsupportedEncodingException;
     26import java.nio.charset.Charset;
     27
     28import com.drew.lang.annotations.NotNull;
     29import com.drew.lang.annotations.Nullable;
     30import com.drew.metadata.StringValue;
    2831
    2932/**
     
    3639 * <ul>
    3740 *     <li>{@link ByteArrayReader}</li>
    38  *     <li>{@link RandomAccessStreamReader}</li>
    3941 * </ul>
    4042 *
     
    4547    private boolean _isMotorolaByteOrder = true;
    4648
     49    public abstract int toUnshiftedOffset(int localOffset);
     50
    4751    /**
    4852     * Gets the byte value at the specified byte <code>index</code>.
     
    5761     * @throws IOException if the byte is unable to be read
    5862     */
    59     protected abstract byte getByte(int index) throws IOException;
     63    public abstract byte getByte(int index) throws IOException;
    6064
    6165    /**
     
    8993     * Returns the length of the data source in bytes.
    9094     * <p>
    91      * This is a simple operation for implementations (such as {@link RandomAccessFileReader} and
     95     * This is a simple operation for implementations (such as
    9296     * {@link ByteArrayReader}) that have the entire data source available.
    9397     * <p>
    94      * Users of this method must be aware that sequentially accessed implementations such as
    95      * {@link RandomAccessStreamReader} will have to read and buffer the entire data source in
     98     * Users of this method must be aware that sequentially accessed implementations
     99     * will have to read and buffer the entire data source in
    96100     * order to determine the length.
    97101     *
     
    207211        if (_isMotorolaByteOrder) {
    208212            // Motorola - MSB first
    209             return (short) (((short)getByte(index    ) << 8 & (short)0xFF00) |
    210                             ((short)getByte(index + 1)      & (short)0xFF));
     213            return (short) ((getByte(index    ) << 8 & (short)0xFF00) |
     214                            (getByte(index + 1)      & (short)0xFF));
    211215        } else {
    212216            // Intel ordering - LSB first
    213             return (short) (((short)getByte(index + 1) << 8 & (short)0xFF00) |
    214                             ((short)getByte(index    )      & (short)0xFF));
     217            return (short) ((getByte(index + 1) << 8 & (short)0xFF00) |
     218                            (getByte(index    )      & (short)0xFF));
    215219        }
    216220    }
     
    229233        if (_isMotorolaByteOrder) {
    230234            // Motorola - MSB first (big endian)
    231             return (((int)getByte(index    )) << 16 & 0xFF0000) |
    232                    (((int)getByte(index + 1)) << 8  & 0xFF00) |
    233                    (((int)getByte(index + 2))       & 0xFF);
     235            return ((getByte(index    )) << 16 & 0xFF0000) |
     236                   ((getByte(index + 1)) << 8  & 0xFF00) |
     237                   ((getByte(index + 2))       & 0xFF);
    234238        } else {
    235239            // Intel ordering - LSB first (little endian)
    236             return (((int)getByte(index + 2)) << 16 & 0xFF0000) |
    237                    (((int)getByte(index + 1)) << 8  & 0xFF00) |
    238                    (((int)getByte(index    ))       & 0xFF);
     240            return ((getByte(index + 2)) << 16 & 0xFF0000) |
     241                   ((getByte(index + 1)) << 8  & 0xFF00) |
     242                   ((getByte(index    ))       & 0xFF);
    239243        }
    240244    }
     
    256260                   (((long)getByte(index + 1)) << 16 & 0xFF0000L) |
    257261                   (((long)getByte(index + 2)) << 8  & 0xFF00L) |
    258                    (((long)getByte(index + 3))       & 0xFFL);
     262                   ((getByte(index + 3))       & 0xFFL);
    259263        } else {
    260264            // Intel ordering - LSB first (little endian)
     
    262266                   (((long)getByte(index + 2)) << 16 & 0xFF0000L) |
    263267                   (((long)getByte(index + 1)) << 8  & 0xFF00L) |
    264                    (((long)getByte(index    ))       & 0xFFL);
     268                   ((getByte(index    ))       & 0xFFL);
    265269        }
    266270    }
     
    312316                   ((long)getByte(index + 5) << 16 & 0xFF0000L) |
    313317                   ((long)getByte(index + 6) << 8  & 0xFF00L) |
    314                    ((long)getByte(index + 7)       & 0xFFL);
     318                   (getByte(index + 7)       & 0xFFL);
    315319        } else {
    316320            // Intel ordering - LSB first
     
    322326                   ((long)getByte(index + 2) << 16 & 0xFF0000L) |
    323327                   ((long)getByte(index + 1) << 8  & 0xFF00L) |
    324                    ((long)getByte(index    )       & 0xFFL);
     328                   (getByte(index    )       & 0xFFL);
    325329        }
    326330    }
     
    365369
    366370    @NotNull
    367     public String getString(int index, int bytesRequested) throws IOException
    368     {
    369         return new String(getBytes(index, bytesRequested));
    370     }
    371 
    372     @NotNull
    373     public String getString(int index, int bytesRequested, String charset) throws IOException
     371    public StringValue getStringValue(int index, int bytesRequested, @Nullable Charset charset) throws IOException
     372    {
     373        return new StringValue(getBytes(index, bytesRequested), charset);
     374    }
     375
     376    @NotNull
     377    public String getString(int index, int bytesRequested, @NotNull Charset charset) throws IOException
     378    {
     379        return new String(getBytes(index, bytesRequested), charset.name());
     380    }
     381
     382    @NotNull
     383    public String getString(int index, int bytesRequested, @NotNull String charset) throws IOException
    374384    {
    375385        byte[] bytes = getBytes(index, bytesRequested);
     
    392402     */
    393403    @NotNull
    394     public String getNullTerminatedString(int index, int maxLengthBytes) throws IOException
    395     {
    396         // NOTE currently only really suited to single-byte character strings
    397 
    398         byte[] bytes = getBytes(index, maxLengthBytes);
     404    public String getNullTerminatedString(int index, int maxLengthBytes, @NotNull Charset charset) throws IOException
     405    {
     406        return new String(getNullTerminatedBytes(index, maxLengthBytes), charset.name());
     407    }
     408
     409    @NotNull
     410    public StringValue getNullTerminatedStringValue(int index, int maxLengthBytes, @Nullable Charset charset) throws IOException
     411    {
     412        byte[] bytes = getNullTerminatedBytes(index, maxLengthBytes);
     413
     414        return new StringValue(bytes, charset);
     415    }
     416
     417    /**
     418     * Returns the sequence of bytes punctuated by a <code>\0</code> value.
     419     *
     420     * @param index The index within the buffer at which to start reading the string.
     421     * @param maxLengthBytes The maximum number of bytes to read. If a <code>\0</code> byte is not reached within this limit,
     422     * the returned array will be <code>maxLengthBytes</code> long.
     423     * @return The read byte array, excluding the null terminator.
     424     * @throws IOException The buffer does not contain enough bytes to satisfy this request.
     425     */
     426    @NotNull
     427    public byte[] getNullTerminatedBytes(int index, int maxLengthBytes) throws IOException
     428    {
     429        byte[] buffer = getBytes(index, maxLengthBytes);
    399430
    400431        // Count the number of non-null bytes
    401432        int length = 0;
    402         while (length < bytes.length && bytes[length] != '\0')
     433        while (length < buffer.length && buffer[length] != 0)
    403434            length++;
    404435
    405         return new String(bytes, 0, length);
     436        if (length == maxLengthBytes)
     437            return buffer;
     438
     439        byte[] bytes = new byte[length];
     440        if (length > 0)
     441            System.arraycopy(buffer, 0, bytes, 0, length);
     442        return bytes;
    406443    }
    407444}
Note: See TracChangeset for help on using the changeset viewer.