static { try { TIME_STAMP_DELIMITER = ": ".getBytes(CharsetSupport.getCharset()); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
private void writeMessage(FileOutputStream stream, String message, boolean forceTimestamp) { try { if (forceTimestamp || includeTimestampForMessages) { writeTimeStamp(stream); } stream.write(message.getBytes(CharsetSupport.getCharset())); stream.write('\n'); stream.flush(); if (syncAfterWrite) { stream.getFD().sync(); } } catch (IOException e) { // QFJ-459: no point trying to log the error in the file if we had an IOException // we will end up with a java.lang.StackOverflowError System.err.println("error writing message to log : " + message); e.printStackTrace(System.err); } }
/** * Calculates the checksum for the given data. * * @param charset the charset used in encoding the data * @param data the data to calculate the checksum on * @param isEntireMessage specifies whether the data is an entire message; if true, and it ends * with a checksum field, that checksum field is excluded from the current checksum * calculation * @return the calculated checksum */ public static int checksum(Charset charset, String data, boolean isEntireMessage) { int sum = 0; if (CharsetSupport.isStringEquivalent(charset)) { // optimization - skip encoding int end = isEntireMessage ? data.lastIndexOf("\00110=") : -1; int len = end > -1 ? end + 1 : data.length(); for (int i = 0; i < len; i++) { sum += data.charAt(i); } } else { byte[] bytes = data.getBytes(charset); int len = bytes.length; if (isEntireMessage && bytes[len - 8] == '\001' && bytes[len - 7] == '1' && bytes[len - 6] == '0' && bytes[len - 5] == '=') len = len - 7; for (int i = 0; i < len; i++) { sum += (bytes[i] & 0xFF); } } return sum & 0xFF; // better than sum % 256 since it avoids overflow issues }
/** * Calculates the length of the byte representation of the given string in the given charset. * * @param charset the charset used in encoding the data * @param data the data to calculate the length on * @return the calculated length */ public static int length(Charset charset, String data) { return CharsetSupport.isStringEquivalent(charset) ? data.length() : data.getBytes(charset).length; }
/** * Calculates the checksum for the given message (excluding existing checksum field, if one * exists). The {@link CharsetSupport#setCharset global charset} is used. * * @param message the message to calculate the checksum on * @return the calculated checksum */ public static int checksum(String message) { return checksum(CharsetSupport.getCharsetInstance(), message, true); }
private void writeTimeStamp(OutputStream out) throws IOException { String formattedTime = UtcTimestampConverter.convert(SystemTime.getDate(), includeMillis); out.write(formattedTime.getBytes(CharsetSupport.getCharset())); out.write(TIME_STAMP_DELIMITER); }