![JAR search and dependency download from the Maven repository](/logo.png)
com.github.powerlibraries.io.helper.stringout.StringBuilderOutputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iopower Show documentation
Show all versions of iopower Show documentation
Power Libraries is a small project to collect some repeatedly needed or otherwise useful Java 8 classes in a collection of tiny libraries.
IO Power is the first and really tiny library of the Power Libraries. It contains some simple helper method for opening Input- and Outputstreams. The main purpose of IO Power is to make opening streams, readers and writers less cluttered and simple to understand.
The newest version!
package com.github.powerlibraries.io.helper.stringout;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
/**
* This class is a simple extension of an {@link ByteArrayOutputStream} that also
* stores a charset to simply create a string from its byte buffer
* @author Manuel Hegner
*
*/
public class StringBuilderOutputStream extends ByteArrayOutputStream {
private Charset charset;
public StringBuilderOutputStream(Charset charset) {
this.charset=charset;
}
/**
* This method returns the string build by this chain.
* @return the string built by this output chain
*/
public String getResult() {
closeSilently();
return new String(this.toByteArray(), charset);
}
/**
* This method returns the string build by this chain.
* @param charset the charset that is used to build the string
* @return the string built by this output chain
*/
public String getResult(Charset charset) {
closeSilently();
return new String(this.toByteArray(), charset);
}
/**
* Closes this Closable without throwing any exceptions.
*/
private void closeSilently() {
try {
close();
} catch(Exception e) {}
}
/**
* Sets the charset of this stream that is used by the getResult methods.
* @param charset the {@link Charset}
*/
public void setCharset(Charset charset) {
this.charset=charset;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy