All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fcrepo.utilities.ReadableByteArrayOutputStream Maven / Gradle / Ivy

There is a newer version: 6.5.0
Show newest version
package org.fcrepo.utilities;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;


public class ReadableByteArrayOutputStream extends ByteArrayOutputStream {

    boolean closed;
    
    public ReadableByteArrayOutputStream() {
        super();
        closed = false;
    }
    
    public ReadableByteArrayOutputStream(int size) {
        super(size);
        closed = false;
    }
    
    @Override
    public void write(byte[] b, int off, int len) {
        if (closed) {
            throw new IllegalStateException("ReadableByteArrayOutputStream has been closed for reading");
        }
        super.write(b, off, len);
    }
    
    @Override
    public void write(int b) {
        if (closed) {
            throw new IllegalStateException("ReadableByteArrayOutputStream has been closed for reading");
        }
        super.write(b);
    }
    
    @Override
    public void close() {
        closed = true;
    }
    
    public int length() {
        return count;
    }
    
    public void writeAllTo(OutputStream out) throws IOException {
        out.write(buf, 0, count);
    }
    
    public ByteArrayInputStream toInputStream() {
        closed = true;
        return new ByteArrayInputStream(buf, 0, count);
    }
    
    public String getString(Charset cs) {
        closed = true;
        return new String(buf, 0, count, cs);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy