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

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

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

import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.CharBuffer;


public class ReadableCharArrayWriter extends CharArrayWriter {
    
    private boolean closed = false;
    
    /**
     * {@inheritDoc}
     */
    public ReadableCharArrayWriter() {
        super();
    }
    
    /**
     * {@inheritDoc}
     */
    public ReadableCharArrayWriter(int size) {
        super(size);
    }
    
    @Override
    public void write(char[] b, int off, int len) {
        if (closed) {
            throw new IllegalStateException("ReadableCharArrayWriter has been closed for reading");
        }
        super.write(b, off, len);
    }
    
    @Override
    public void write(int b) {
        if (closed) {
            throw new IllegalStateException("ReadableCharArrayWriter has been closed for reading");
        }
        super.write(b);
    }
    
    @Override
    public void close() {
        closed = true;
    }
    
    public int length() {
        return count;
    }
    
    public void writeAllTo(Writer writer) throws IOException {
        writer.write(buf, 0, count);
    }
    
    public CharArrayReader toReader() {
        closed = true;
        return new CharArrayReader(buf, 0, count);
    }
    
    public CharBuffer toBuffer() {
        closed = true;
        return CharBuffer.wrap(buf, 0, count).asReadOnlyBuffer();
    }
    
    public String getString() {
        closed = true;
        return new String(buf, 0, count);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy