Skip to content

Commit

Permalink
Merge pull request #492 from franz1981/small_improvements
Browse files Browse the repository at this point in the history
Save head encoding on sanitized String(s)
  • Loading branch information
jamezp authored Nov 15, 2024
2 parents 6489b41 + 5dc38c8 commit 8334439
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public TextBannerFormatter(final Supplier<String> bannerSupplier, final ExtForma
public String getHead(final Handler h) {
final String dh = Objects.requireNonNullElse(delegate.getHead(h), "");
final String banner = Objects.requireNonNullElse(bannerSupplier.get(), "");
return banner + dh;
// it doesn't use + because dh can be empty and we both don't want to create an indy and
// we don't want to create a new string
return banner.concat(dh);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ protected void setCharsetPrivate(Charset charset) throws SecurityException {
}
}

public Charset getCharset() {
lock.lock();
try {
return super.getCharset();
} finally {
lock.unlock();
}
}

/** {@inheritDoc} Setting a writer will replace any target output stream. */
public void setWriter(final Writer writer) {
lock.lock();
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/jboss/logmanager/handlers/WriterHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.Closeable;
import java.io.Flushable;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.ErrorManager;
import java.util.logging.Formatter;

Expand Down Expand Up @@ -175,9 +177,11 @@ private void writeHead(final Writer writer) {
final Formatter formatter = getFormatter();
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,
if (!head.isEmpty() && checkHeadEncoding) {
Charset cs = getCharset();
// UTF-8 is always safe since the UTF-16 chars in String(s) are always encodable
if (!StandardCharsets.UTF_8.equals(cs) && cs.newEncoder().canEncode(head)) {
reportError("Section header cannot be encoded into charset \"" + cs.name() + "\"", null,
ErrorManager.GENERIC_FAILURE);
return;
}
Expand Down

0 comments on commit 8334439

Please sign in to comment.