写入具有容量限制的OutputStream

Yak*_*kov 2 java outputstream capacity

在我之前提出的问题之后:我正在实施ByteArrayOutputStream容量限制.我的主要限制是可用内存量.所以有这样的流os:

  1. 当我写1MB输出流以上时,我需要"停止".我不想抛出异常,而是将os 输出流的完整内容写入指定的其他输出流参数. OutputStream out; os.writeTo(out); 然后继续os从一开始就写作

  2. 为了防止1中描述的情况,我倾向于os尽可能频繁地消耗.我的意思是将数据从它复制到out512KB的块中是否可行?如果是,任何建议怎么办?或者可能有一个内置的课程来满足我的要求

编辑:写入的字节out数也是有限的.我可以在那里写高达1GB.如果我有更多,我需要创建其他输出流,以便从os那里流失.写入操作系统的过程.可以那样.在那里写了500MB - 我将它立即转移到了外面.几秒钟之后就写了700MB - 我需要只消耗500MB到out其他200MB到其他输出流(out2),我需要在这种情况下创建

Thi*_*rry 6

您所描述的是BufferedOutputStream,您可以像这样构造:

new BufferedOutputStream(out, 512000)
Run Code Online (Sandbox Code Playgroud)

第一个arg是你拥有的另一个输出流,第二个是BufferedOutputStream内部缓冲区的大小

编辑:

好的,我一开始并不完全了解你的需求.您确实需要扩展OutputStream来实现这一点.这是一个示例代码:

以下是如何使用以下代码:

    public static void main(String[] args) throws IOException {
        AtomicLong idx = new AtomicLong(0);
        try (
            OutputStream out = new OutputStreamMultiVolume(10, () -> new FileOutputStream(getNextFilename(idx)));
            ) {

            out.write("01234567890123456789012345678901234567890123456789".getBytes("UTF-8"));
        }
    }

    private static File getNextFilename(AtomicLong idx) {
        return new File("sample.file." + idx.incrementAndGet() + ".txt");
    }
Run Code Online (Sandbox Code Playgroud)

OutputStreamMultiVolume的第一个构造函数arg是卷的最大大小.如果我们达到这个大小,我们将关闭当前的outputStream,并调用OutputStreamSupplier来获取下一个.

这里的示例代码将String 01234567890123456789012345678901234567890123456789(5次0123456789)写入名为'sample.file.idx.txt'的文件中,每当我们达到最大流量大小时,idx就会增加(因此你将获得5个文件).

和班级自己:

public class OutputStreamMultiVolume extends OutputStream {

    private final long maxBytePerVolume;
    private long bytesInCurrentVolume = 0;
    private OutputStream out;
    private OutputStreamSupplier outputStreamSupplier;

    static interface OutputStreamSupplier {
        OutputStream get() throws IOException;
    }

    public OutputStreamMultiVolume(long maxBytePerOutput, OutputStreamSupplier outputStreamSupplier) throws IOException {
        this.outputStreamSupplier = outputStreamSupplier;
        this.maxBytePerVolume = maxBytePerOutput;
        this.out = outputStreamSupplier.get();
    }

    @Override
    public synchronized void write(byte[] bytes) throws IOException {
        final int remainingBytesInVol = (int) (maxBytePerVolume - bytesInCurrentVolume);
        if (remainingBytesInVol >= bytes.length) {
            out.write(bytes);
            bytesInCurrentVolume += bytes.length;
            return;
        }

        out.write(bytes, 0, remainingBytesInVol);
        switchOutput();

        this.write(bytes, remainingBytesInVol, bytes.length - remainingBytesInVol);
    }

    @Override
    public synchronized void write(int b) throws IOException {
        if (bytesInCurrentVolume + 1 <= maxBytePerVolume) {
            out.write(b);
            bytesInCurrentVolume += 1;
            return;
        }

        switchOutput();
        out.write(b);
        bytesInCurrentVolume += 1;
    }

    @Override
    public synchronized void write(byte[] b, int off, int len) throws IOException {
        final int remainingBytesInVol = (int) (maxBytePerVolume - bytesInCurrentVolume);
        if (remainingBytesInVol >= len) {
            out.write(b, off, len);
            bytesInCurrentVolume += len;
            return;
        }

        out.write(b, off, remainingBytesInVol);
        switchOutput();
        this.write(b, off + remainingBytesInVol, len - remainingBytesInVol);
        bytesInCurrentVolume += len - remainingBytesInVol;
    }

    private void switchOutput() throws IOException {
        out.flush();
        out.close();

        out = outputStreamSupplier.get();
        bytesInCurrentVolume = 0;
    }

    @Override
    public synchronized void close() throws IOException {
        out.close();
    }

    @Override
    public synchronized void flush() throws IOException {
        out.flush();
    }
}
Run Code Online (Sandbox Code Playgroud)