Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make it explicit that a BufferedWriter can be passed in.
  • Loading branch information
johnmay committed Jun 2, 2016
1 parent e78d305 commit aabd1fb
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions storage/io/src/main/java/org/openscience/cdk/io/SDFWriter.java
Expand Up @@ -28,6 +28,7 @@
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -67,29 +68,38 @@ public class SDFWriter extends DefaultChemObjectWriter {
private Set<String> propertiesToWrite;

/**
* Constructs a new SDFWriter that writes to the given {@link Writer}.
* Create an SDfile writer that will output directly to the provided buffered writer.
*
* @param out The {@link Writer} to write to
* @param wtr writer
*/
public SDFWriter(Writer out) {
if (out instanceof BufferedWriter) {
writer = (BufferedWriter) out;
} else {
writer = new BufferedWriter(out);
}
public SDFWriter(BufferedWriter wtr) {
this.writer = wtr;
initIOSettings();
}

/**
* Constructs a new MDLWriter that can write to a given
* {@link OutputStream}.
* Create an SDfile writer, the provided writer is buffered
* if it's not an instance of BufferedWriter. For flush control
* etc please create with {@link BufferedWriter}.
*
* @param wtr writer
*/
public SDFWriter(Writer wtr) {
this(ensureBuffered(wtr));
initIOSettings();
}

/**
* Create an SDfile writer, the provided output stream is wrapped
* in a UTF-8 buffered writer.
*
* @param output The {@link OutputStream} to write to
* @param output out stream
*/
public SDFWriter(OutputStream output) {
this(new OutputStreamWriter(output));
this(new OutputStreamWriter(output, StandardCharsets.UTF_8));
}


public SDFWriter() {
this(new StringWriter());
}
Expand All @@ -100,11 +110,7 @@ public SDFWriter() {
* @param out The {@link Writer} to write to
*/
public SDFWriter(Writer out, Set<String> propertiesToWrite) {
if (out instanceof BufferedWriter) {
writer = (BufferedWriter) out;
} else {
writer = new BufferedWriter(out);
}
this(out);
initIOSettings();
this.propertiesToWrite = propertiesToWrite;
}
Expand All @@ -126,6 +132,18 @@ public SDFWriter(Set<String> propertiesToWrite) {
this(new StringWriter(), propertiesToWrite);
}

/**
* Ensures a writer is buffered.
*
* @param wtr writer, may be buffered
* @return a BufferedWriter
*/
private static BufferedWriter ensureBuffered(Writer wtr) {
if (wtr == null) throw new NullPointerException("Provided writer was null");
return wtr instanceof BufferedWriter ? (BufferedWriter) wtr
: new BufferedWriter(wtr);
}

@Override
public IResourceFormat getFormat() {
return SDFFormat.getInstance();
Expand Down

0 comments on commit aabd1fb

Please sign in to comment.