Ignore:
Timestamp:
2017-10-30T22:46:09+01:00 (7 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/SequentialReader.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");
     
    2323
    2424import com.drew.lang.annotations.NotNull;
     25import com.drew.lang.annotations.Nullable;
     26import com.drew.metadata.StringValue;
    2527
    2628import java.io.EOFException;
    2729import java.io.IOException;
    2830import java.io.UnsupportedEncodingException;
     31import java.nio.charset.Charset;
    2932
    3033/**
    3134 * @author Drew Noakes https://drewnoakes.com
    3235 */
     36@SuppressWarnings("WeakerAccess")
    3337public abstract class SequentialReader
    3438{
     
    3741    private boolean _isMotorolaByteOrder = true;
    3842
     43    public abstract long getPosition() throws IOException;
     44
    3945    /**
    4046     * Gets the next byte in the sequence.
     
    4248     * @return The read byte value
    4349     */
    44     protected abstract byte getByte() throws IOException;
     50    public abstract byte getByte() throws IOException;
    4551
    4652    /**
     
    5258    @NotNull
    5359    public abstract byte[] getBytes(int count) throws IOException;
     60
     61    /**
     62     * Retrieves bytes, writing them into a caller-provided buffer.
     63     * @param buffer The array to write bytes to.
     64     * @param offset The starting position within buffer to write to.
     65     * @param count The number of bytes to be written.
     66     */
     67    public abstract void getBytes(@NotNull byte[] buffer, int offset, int count) throws IOException;
    5468
    5569    /**
     
    7084     */
    7185    public abstract boolean trySkip(long n) throws IOException;
     86
     87    /**
     88     * Returns an estimate of the number of bytes that can be read (or skipped
     89     * over) from this {@link SequentialReader} without blocking by the next
     90     * invocation of a method for this input stream. A single read or skip of
     91     * this many bytes will not block, but may read or skip fewer bytes.
     92     * <p>
     93     * Note that while some implementations of {@link SequentialReader} like
     94     * {@link SequentialByteArrayReader} will return the total remaining number
     95     * of bytes in the stream, others will not. It is never correct to use the
     96     * return value of this method to allocate a buffer intended to hold all
     97     * data in this stream.
     98     *
     99     * @return an estimate of the number of bytes that can be read (or skipped
     100     *         over) from this {@link SequentialReader} without blocking or
     101     *         {@code 0} when it reaches the end of the input stream.
     102     */
     103    public abstract int available();
    72104
    73105    /**
     
    284316    }
    285317
     318    @NotNull
     319    public String getString(int bytesRequested, @NotNull Charset charset) throws IOException
     320    {
     321        byte[] bytes = getBytes(bytesRequested);
     322        return new String(bytes, charset);
     323    }
     324
     325    @NotNull
     326    public StringValue getStringValue(int bytesRequested, @Nullable Charset charset) throws IOException
     327    {
     328        return new StringValue(getBytes(bytesRequested), charset);
     329    }
     330
    286331    /**
    287332     * Creates a String from the stream, ending where <code>byte=='\0'</code> or where <code>length==maxLength</code>.
     
    293338     */
    294339    @NotNull
    295     public String getNullTerminatedString(int maxLengthBytes) throws IOException
    296     {
    297         // NOTE currently only really suited to single-byte character strings
    298 
    299         byte[] bytes = new byte[maxLengthBytes];
     340    public String getNullTerminatedString(int maxLengthBytes, Charset charset) throws IOException
     341    {
     342       return getNullTerminatedStringValue(maxLengthBytes, charset).toString();
     343    }
     344
     345    /**
     346     * Creates a String from the stream, ending where <code>byte=='\0'</code> or where <code>length==maxLength</code>.
     347     *
     348     * @param maxLengthBytes The maximum number of bytes to read.  If a <code>\0</code> byte is not reached within this limit,
     349     *                       reading will stop and the string will be truncated to this length.
     350     * @param charset The <code>Charset</code> to register with the returned <code>StringValue</code>, or <code>null</code> if the encoding
     351     *                is unknown
     352     * @return The read string.
     353     * @throws IOException The buffer does not contain enough bytes to satisfy this request.
     354     */
     355    @NotNull
     356    public StringValue getNullTerminatedStringValue(int maxLengthBytes, Charset charset) throws IOException
     357    {
     358        byte[] bytes = getNullTerminatedBytes(maxLengthBytes);
     359
     360        return new StringValue(bytes, charset);
     361    }
     362
     363    /**
     364     * Returns the sequence of bytes punctuated by a <code>\0</code> value.
     365     *
     366     * @param maxLengthBytes The maximum number of bytes to read. If a <code>\0</code> byte is not reached within this limit,
     367     * the returned array will be <code>maxLengthBytes</code> long.
     368     * @return The read byte array, excluding the null terminator.
     369     * @throws IOException The buffer does not contain enough bytes to satisfy this request.
     370     */
     371    @NotNull
     372    public byte[] getNullTerminatedBytes(int maxLengthBytes) throws IOException
     373    {
     374        byte[] buffer = new byte[maxLengthBytes];
    300375
    301376        // Count the number of non-null bytes
    302377        int length = 0;
    303         while (length < bytes.length && (bytes[length] = getByte()) != '\0')
     378        while (length < buffer.length && (buffer[length] = getByte()) != 0)
    304379            length++;
    305380
    306         return new String(bytes, 0, length);
     381        if (length == maxLengthBytes)
     382            return buffer;
     383
     384        byte[] bytes = new byte[length];
     385        if (length > 0)
     386            System.arraycopy(buffer, 0, bytes, 0, length);
     387        return bytes;
    307388    }
    308389}
Note: See TracChangeset for help on using the changeset viewer.