Skip to content

Commit

Permalink
Feature: Adds byte[] capability to append_content() function
Browse files Browse the repository at this point in the history
  • Loading branch information
vigorouscoding committed Apr 9, 2020
1 parent 0390805 commit 88e8cb6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,29 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]
if (sources[0] instanceof File) {

final File file = (File)sources[0];
final String content = (String)sources[1];
final String encoding = (sources.length == 3 && sources[2] != null) ? sources[2].toString() : "UTF-8";

try (final FileOutputStream fos = file.getOutputStream(true, true)) {
if (sources[1] instanceof byte[]) {

fos.write(content.getBytes(encoding));
try (final FileOutputStream fos = file.getOutputStream(true, true)) {

} catch (IOException ioex) {
logger.warn("append_content(): Unable to append to file '{}'", file.getPath(), ioex);
fos.write((byte[]) sources[1]);

} catch (IOException ioex) {
logger.warn("append_content(): Unable to append binary data to file '{}'", file.getPath(), ioex);
}

} else {

final String content = (String)sources[1];

try (final FileOutputStream fos = file.getOutputStream(true, true)) {

fos.write(content.getBytes(encoding));

} catch (IOException ioex) {
logger.warn("append_content(): Unable to append to file '{}'", file.getPath(), ioex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]

final File file = (File)sources[0];
final String encoding = (sources.length == 3 && sources[2] != null) ? sources[2].toString() : "UTF-8";

if (sources[1] instanceof byte[]) {

try (final FileOutputStream fos = file.getOutputStream(true, false)) {

fos.write((byte[]) sources[1]);
Expand All @@ -64,9 +64,9 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]
}

} else {
final String content = (String)sources[1];

final String content = (String)sources[1];

try (final FileOutputStream fos = file.getOutputStream(true, false)) {

fos.write(content.getBytes(encoding));
Expand All @@ -75,8 +75,6 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]
logger.warn("set_content(): Unable to write content to file '{}'", file.getPath(), ioex);
}
}


}

} catch (ArgumentNullException pe) {
Expand Down

0 comments on commit 88e8cb6

Please sign in to comment.