How about something like this: diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java index a48876f..d6e77ef 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java @@ -16,7 +16,7 @@ */ package org.apache.logging.log4j.core.appender;
-import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.apache.logging.log4j.util.Chars; @@ -26,46 +26,56 @@ * @see <a href="https://tools.ietf.org/html/rfc5425">RFC 5425</a> */ public class TlsSyslogFrame { - private String message; - private int messageLengthInBytes; + private final String message; public TlsSyslogFrame(final String message) { - setMessage(message); + this.message = message; } public String getMessage() { return this.message; } - public void setMessage(final String message) { - this.message = message; - setLengthInBytes(); - } - - private void setLengthInBytes() { - messageLengthInBytes = message.length(); - } - public byte[] getBytes() { - final String frame = toString(); - return frame.getBytes(Charset.defaultCharset()); + final String frame = toString(); + return frame.getBytes(StandardCharsets.UTF_8); } @Override public String toString() { - final String length = Integer.toString(messageLengthInBytes); + final byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8); + final String length = Integer.toString(messageBytes.length); return length + Chars.SPACE + message; } - public boolean equals(final TlsSyslogFrame frame) { - return isLengthEquals(frame) && isMessageEquals(frame); + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + return result; } - private boolean isLengthEquals(final TlsSyslogFrame frame) { - return this.messageLengthInBytes == frame.messageLengthInBytes; + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof TlsSyslogFrame)) { + return false; + } + final TlsSyslogFrame other = (TlsSyslogFrame) obj; + if (message == null) { + if (other.message != null) { + return false; + } + } else if (!message.equals(other.message)) { + return false; + } + return true; } - private boolean isMessageEquals(final TlsSyslogFrame frame) { - return this.message.equals(frame.message); - } } Gary On Wed, Jan 20, 2016 at 6:27 PM, Gary Gregory <[email protected]> wrote: > And another issue: TlsSyslogFrame implements equals() but not hashCode(). > > Gary > > On Wed, Jan 20, 2016 at 4:58 PM, Blake Day <[email protected]> wrote: > >> TlsSyslogFrame appears to be miscalculating the message length. >> According to RFC5425, the message length must be the octet count of the >> SYSLOG-MSG in the frame. Though the variable below is aptly named >> messageLengthInBytes, it is assigned the value from message.length() (where >> message is a String) rather than the bytes. >> >> See below for relevant portions of code: >> >> private String message; >> private int messageLengthInBytes; >> >> private void setLengthInBytes() { >> messageLengthInBytes = message.length(); >> } >> >> @Override >> public String toString() { >> final String length = Integer.toString(messageLengthInBytes); >> return length + Chars.SPACE + message; >> } >> >> >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> >> > > > -- > E-Mail: [email protected] | [email protected] > Java Persistence with Hibernate, Second Edition > <http://www.manning.com/bauer3/> > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/> > Spring Batch in Action <http://www.manning.com/templier/> > Blog: http://garygregory.wordpress.com > Home: http://garygregory.com/ > Tweet! http://twitter.com/GaryGregory > -- E-Mail: [email protected] | [email protected] Java Persistence with Hibernate, Second Edition <http://www.manning.com/bauer3/> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/> Spring Batch in Action <http://www.manning.com/templier/> Blog: http://garygregory.wordpress.com Home: http://garygregory.com/ Tweet! http://twitter.com/GaryGregory
