Skip to content

Commit

Permalink
Optionally check for, and report, head/tail encoding problems
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Mar 13, 2020
1 parent cd46f32 commit 04acfb0
Showing 1 changed file with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
public class WriterHandler extends ExtHandler {

protected final Object outputLock = new Object();
private volatile boolean checkHeadEncoding = true;
private volatile boolean checkTailEncoding = true;
private Writer writer;

/**
Expand Down Expand Up @@ -118,10 +120,59 @@ public void setWriter(final Writer writer) {
}
}

/**
* Determine whether head encoding checking is turned on.
*
* @return {@code true} to check and report head encoding problems, or {@code false} to ignore them
*/
public boolean isCheckHeadEncoding() {
return checkHeadEncoding;
}

/**
* Establish whether head encoding checking is turned on.
*
* @param checkHeadEncoding {@code true} to check and report head encoding problems, or {@code false} to ignore them
* @return this handler
*/
public WriterHandler setCheckHeadEncoding(boolean checkHeadEncoding) {
this.checkHeadEncoding = checkHeadEncoding;
return this;
}

/**
* Determine whether tail encoding checking is turned on.
*
* @return {@code true} to check and report tail encoding problems, or {@code false} to ignore them
*/
public boolean isCheckTailEncoding() {
return checkTailEncoding;
}

/**
* Establish whether tail encoding checking is turned on.
*
* @param checkTailEncoding {@code true} to check and report tail encoding problems, or {@code false} to ignore them
* @return this handler
*/
public WriterHandler setCheckTailEncoding(boolean checkTailEncoding) {
this.checkTailEncoding = checkTailEncoding;
return this;
}

private void writeHead(final Writer writer) {
try {
final Formatter formatter = getFormatter();
if (formatter != null) writer.write(formatter.getHead(this));
if (formatter != null) {
final String head = formatter.getHead(this);
if (checkHeadEncoding) {
if (!getCharset().newEncoder().canEncode(head)) {
reportError("Section header cannot be encoded into charset \"" + getCharset().name() + "\"", null, ErrorManager.GENERIC_FAILURE);
return;
}
}
writer.write(head);
}
} catch (Exception e) {
reportError("Error writing section header", e, ErrorManager.WRITE_FAILURE);
}
Expand All @@ -130,7 +181,16 @@ private void writeHead(final Writer writer) {
private void writeTail(final Writer writer) {
try {
final Formatter formatter = getFormatter();
if (formatter != null) writer.write(formatter.getTail(this));
if (formatter != null) {
final String tail = formatter.getTail(this);
if (checkTailEncoding) {
if (!getCharset().newEncoder().canEncode(tail)) {
reportError("Section tail cannot be encoded into charset \"" + getCharset().name() + "\"", null, ErrorManager.GENERIC_FAILURE);
return;
}
}
writer.write(tail);
}
} catch (Exception ex) {
reportError("Error writing section tail", ex, ErrorManager.WRITE_FAILURE);
}
Expand Down

0 comments on commit 04acfb0

Please sign in to comment.