From 2d8a4a9f8a08452d005bca01ae4f8923aa69968b Mon Sep 17 00:00:00 2001 From: zml2008 Date: Mon, 26 Dec 2011 16:43:33 -0800 Subject: [PATCH] Allow using custom input and output streams in subclasses of YAMLProcessor. --- .../com/sk89q/util/yaml/YAMLProcessor.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java b/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java index e513d25d7..7c742255a 100644 --- a/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java +++ b/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java @@ -25,11 +25,7 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.reader.UnicodeReader; import org.yaml.snakeyaml.representer.Representer; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; +import java.io.*; import java.util.HashMap; import java.util.Map; @@ -95,10 +91,11 @@ public class YAMLProcessor extends YAMLNode { * @throws java.io.IOException */ public void load() throws IOException { - FileInputStream stream = null; + InputStream stream = null; try { - stream = new FileInputStream(file); + stream = getInputStream(); + if (stream == null) throw new IOException("Stream is null!"); read(yaml.load(new UnicodeReader(stream))); } catch (YAMLProcessorException e) { root = new HashMap(); @@ -158,7 +155,7 @@ public class YAMLProcessor extends YAMLNode { * @return true if it was successful */ public boolean save() { - FileOutputStream stream = null; + OutputStream stream = null; File parent = file.getParentFile(); @@ -167,7 +164,8 @@ public class YAMLProcessor extends YAMLNode { } try { - stream = new FileOutputStream(file); + stream = getOutputStream(); + if (stream == null) return false; OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8"); if (header != null) { writer.append(header); @@ -200,6 +198,14 @@ public class YAMLProcessor extends YAMLNode { } } + public InputStream getInputStream() throws IOException { + return new FileInputStream(file); + } + + public OutputStream getOutputStream() throws IOException { + return new FileOutputStream(file); + } + /** * This method returns an empty ConfigurationNode for using as a * default in methods that select a node from a node list.