Skip to content

Commit

Permalink
[#119] Do not close the given stream in DebugStringUtil.toJsonStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed May 6, 2022
1 parent 38496a6 commit af81bb0
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zserio.runtime;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
Expand Down Expand Up @@ -30,11 +30,10 @@ public class DebugStringUtil
*/
public static void toJsonStream(Object zserioObject, Writer writer, int indent, WalkFilter walkFilter)
{
try (final JsonWriter jsonWriter = new JsonWriter(writer, indent))
{
final Walker walker = new Walker(jsonWriter, walkFilter);
walker.walk(zserioObject);
}
// do not close the output stream to allow usage e.g. of System.out
final JsonWriter jsonWriter = new JsonWriter(writer, indent);
final Walker walker = new Walker(jsonWriter, walkFilter);
walker.walk(zserioObject);
}

/**
Expand Down Expand Up @@ -134,14 +133,18 @@ public static String toJsonString(Object zserioObject)
* @param indent Indent argument for JsonWriter.
* @param walkFilter WalkFilter to use by Walker.
*
* @throws FileNotFoundException When the output file cannot be created.
* @throws IOException When the output file cannot be created correctly.
*/
public static void toJsonFile(Object zserioObject, String fileName, int indent, WalkFilter walkFilter)
throws FileNotFoundException
throws IOException
{
final OutputStream outputStream = new FileOutputStream(fileName);
final OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
toJsonStream(zserioObject, writer, indent, walkFilter);
try (
final OutputStream outputStream = new FileOutputStream(fileName);
final OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
)
{
toJsonStream(zserioObject, writer, indent, walkFilter);
}
}

/**
Expand All @@ -151,9 +154,9 @@ public static void toJsonFile(Object zserioObject, String fileName, int indent,
* @param fileName Name of file to write.
* @param indent Indent argument for JsonWriter.
*
* @throws FileNotFoundException When the output file cannot be created.
* @throws IOException When the output file cannot be created correctly.
*/
public static void toJsonFile(Object zserioObject, String fileName, int indent) throws FileNotFoundException
public static void toJsonFile(Object zserioObject, String fileName, int indent) throws IOException
{
toJsonFile(zserioObject, fileName, indent, new DefaultWalkFilter());
}
Expand All @@ -165,10 +168,10 @@ public static void toJsonFile(Object zserioObject, String fileName, int indent)
* @param fileName Name of file to write.
* @param walkFilter WalkFilter to use by Walker.
*
* @throws FileNotFoundException When the output file cannot be created.
* @throws IOException When the output file cannot be created correctly.
*/
public static void toJsonFile(Object zserioObject, String fileName, WalkFilter walkFilter)
throws FileNotFoundException
throws IOException
{
toJsonFile(zserioObject, fileName, DEFAULT_INDENT, walkFilter);
}
Expand All @@ -179,9 +182,9 @@ public static void toJsonFile(Object zserioObject, String fileName, WalkFilter w
* @param zserioObject Zserio object to use.
* @param fileName Name of file to write.
*
* @throws FileNotFoundException When the output file cannot be created.
* @throws IOException When the output file cannot be created correctly.
*/
public static void toJsonFile(Object zserioObject, String fileName) throws FileNotFoundException
public static void toJsonFile(Object zserioObject, String fileName) throws IOException
{
toJsonFile(zserioObject, fileName, DEFAULT_INDENT);
}
Expand Down

0 comments on commit af81bb0

Please sign in to comment.