20

InputStreamをファイルにコピーしようとしましたが、InputStreamのサイズが1MBを超える場合はコピーを中止しました。Java7では、次のようにコードを記述しました。

public void copy(InputStream input, Path target) {
    OutputStream out = Files.newOutputStream(target,
            StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
    boolean isExceed = false;
    try {
        long nread = 0L;
        byte[] buf = new byte[BUFFER_SIZE];
        int n;
        while ((n = input.read(buf)) > 0) {
            out.write(buf, 0, n);
            nread += n;
            if (nread > 1024 * 1024) {// Exceed 1 MB
                isExceed = true;
                break;
            }
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        out.close();
        if (isExceed) {// Abort the copy
            Files.deleteIfExists(target);
            throw new IllegalArgumentException();
        }
    }}
  • 最初の質問:それに対するより良い解決策はありますか?
  • 2番目の質問:他の解決策-コピー操作の前に、このInputStreamのサイズを計算します。そこで、InputStreamをコピーしてByteArrayOutputStream取得しsize()ます。ただし、問題はInputStreamがない可能性がmarkSupported()あるため、InputStreamをファイルのコピー操作で再利用できないことです。
4

6 に答える 6

22

私の個人的な選択は、バイトを読み取るときにバイトをカウントするInputStreamラッパーです。

public class LimitedSizeInputStream extends InputStream {

    private final InputStream original;
    private final long maxSize;
    private long total;

    public LimitedSizeInputStream(InputStream original, long maxSize) {
        this.original = original;
        this.maxSize = maxSize;
    }

    @Override
    public int read() throws IOException {
        int i = original.read();
        if (i>=0) incrementCounter(1);
        return i;
    }

    @Override
    public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte b[], int off, int len) throws IOException {
        int i = original.read(b, off, len);
        if (i>=0) incrementCounter(i);
        return i;
    }

    private void incrementCounter(int size) throws IOException {
        total += size;
        if (total>maxSize) throw new IOException("InputStream exceeded maximum size in bytes.");
    }

}

このアプローチは透過的であり、すべての入力ストリームで再利用可能であり、他のライブラリでもうまく機能するため、私はこのアプローチが好きです。たとえば、ApacheCommonsを使用して最大4KBのファイルをコピーします。

InputStream in = new LimitedSizeInputStream(new FileInputStream("from.txt"), 4096);
OutputStream out = new FileOutputStream("to.txt");
IOUtils.copy(in, out);

PS:BoundedInputStreamを使用した上記の実装の主な違いは、制限を超えてもBoundedInputStreamが例外をスローしないことです(ストリームを閉じるだけです)。

于 2015-05-06T09:02:45.057 に答える
15

これには、次の解決策があります。

于 2015-03-18T12:23:36.190 に答える
3

最初の質問:それに対するより良い解決策はありますか?

あまり。確かに、それほど良くはありません。

2番目の質問:他の解決策-コピー操作の前に、InputStreamのサイズを計算します。そこで、InputStreamをByteArrayOutputStreamにコピーしてから、size()を取得します。ただし、問題は、InputStreamがmarkSupported()をマークしない可能性があるため、InputStreamをファイルのコピー操作で再利用できないことです。

上記は質問ではなくステートメントであることはさておき...

バイトをにコピーした場合は、によって返されたバイト配列からByteArrayOutputStreamを作成できます。したがって、元のストリームをマーク/リセットする必要はありません。ByteArrayInputStreambaos.toByteArray()

しかし、それはこれを実装するためのかなり醜い方法です。とにかく入力ストリーム全体を読み取ってバッファリングしているからです。

于 2013-03-16T04:34:03.130 に答える
2

これは、ApacheTomcatからの実装です。

package org.apache.tomcat.util.http.fileupload.util;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * An input stream, which limits its data size. This stream is
 * used, if the content length is unknown.
 */
public abstract class LimitedInputStream extends FilterInputStream implements Closeable {

    /**
     * The maximum size of an item, in bytes.
     */
    private final long sizeMax;

    /**
     * The current number of bytes.
     */
    private long count;

    /**
     * Whether this stream is already closed.
     */
    private boolean closed;

    /**
     * Creates a new instance.
     *
     * @param inputStream The input stream, which shall be limited.
     * @param pSizeMax The limit; no more than this number of bytes
     *   shall be returned by the source stream.
     */
    public LimitedInputStream(InputStream inputStream, long pSizeMax) {
        super(inputStream);
        sizeMax = pSizeMax;
    }

    /**
     * Called to indicate, that the input streams limit has
     * been exceeded.
     *
     * @param pSizeMax The input streams limit, in bytes.
     * @param pCount The actual number of bytes.
     * @throws IOException The called method is expected
     *   to raise an IOException.
     */
    protected abstract void raiseError(long pSizeMax, long pCount)
            throws IOException;

    /**
     * Called to check, whether the input streams
     * limit is reached.
     *
     * @throws IOException The given limit is exceeded.
     */
    private void checkLimit() throws IOException {
        if (count > sizeMax) {
            raiseError(sizeMax, count);
        }
    }

    /**
     * Reads the next byte of data from this input stream. The value
     * byte is returned as an <code>int</code> in the range
     * <code>0</code> to <code>255</code>. If no byte is available
     * because the end of the stream has been reached, the value
     * <code>-1</code> is returned. This method blocks until input data
     * is available, the end of the stream is detected, or an exception
     * is thrown.
     * <p>
     * This method
     * simply performs <code>in.read()</code> and returns the result.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             stream is reached.
     * @throws  IOException  if an I/O error occurs.
     * @see        java.io.FilterInputStream#in
     */
    @Override
    public int read() throws IOException {
        int res = super.read();
        if (res != -1) {
            count++;
            checkLimit();
        }
        return res;
    }

    /**
     * Reads up to <code>len</code> bytes of data from this input stream
     * into an array of bytes. If <code>len</code> is not zero, the method
     * blocks until some input is available; otherwise, no
     * bytes are read and <code>0</code> is returned.
     * <p>
     * This method simply performs <code>in.read(b, off, len)</code>
     * and returns the result.
     *
     * @param      b     the buffer into which the data is read.
     * @param      off   The start offset in the destination array
     *                   <code>b</code>.
     * @param      len   the maximum number of bytes read.
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the stream has been reached.
     * @throws  NullPointerException If <code>b</code> is <code>null</code>.
     * @throws  IndexOutOfBoundsException If <code>off</code> is negative,
     * <code>len</code> is negative, or <code>len</code> is greater than
     * <code>b.length - off</code>
     * @throws  IOException  if an I/O error occurs.
     * @see        java.io.FilterInputStream#in
     */
    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int res = super.read(b, off, len);
        if (res > 0) {
            count += res;
            checkLimit();
        }
        return res;
    }

    /**
     * Returns, whether this stream is already closed.
     *
     * @return True, if the stream is closed, otherwise false.
     * @throws IOException An I/O error occurred.
     */
    @Override
    public boolean isClosed() throws IOException {
        return closed;
    }

    /**
     * Closes this input stream and releases any system resources
     * associated with the stream.
     * This
     * method simply performs <code>in.close()</code>.
     *
     * @throws  IOException  if an I/O error occurs.
     * @see        java.io.FilterInputStream#in
     */
    @Override
    public void close() throws IOException {
        closed = true;
        super.close();
    }
}

これをサブクラス化し、メソッドをオーバーライドする必要がありますraiseError

于 2019-05-22T09:21:04.480 に答える
1

入力ストリームのサイズをチェックするためのはるかに便利で高速なソリューション  

    FileChannel chanel = (FileChannel) Channels.newChannel(inputStream);
    MappedByteBuffer buffer = chanel.map(FileChannel.MapMode.READ_ONLY, 0, chanel.size());

    System.out.println(buffer.capacity()); // bytes
于 2019-12-19T11:13:47.040 に答える
0

ByteArrayOutputStreamベースのソリューションが好きですが、なぜ機能しないのかわかりません

public void copy(InputStream input, Path target) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BufferedInputStream bis = new BufferedInputStream(input);
    for (int b = 0; (b = bis.read()) != -1;) {
        if (bos.size() > BUFFER_SIZE) {
            throw new IOException();
        }
        bos.write(b);
    }
    Files.write(target, bos.toByteArray());
}
于 2013-03-16T05:20:20.200 に答える