Commit 23d6aabc authored by Dave Cridland's avatar Dave Cridland

Merge pull request #346 from sco0ter/utf8

Use StandardCharsets.UTF_8 with Reader/Writer classes.
parents 1989f919 8b272c67
...@@ -27,6 +27,7 @@ import java.io.FilenameFilter; ...@@ -27,6 +27,7 @@ import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
...@@ -346,7 +347,7 @@ public class AuditorImpl implements Auditor { ...@@ -346,7 +347,7 @@ public class AuditorImpl implements Auditor {
currentAuditFile = tmpAuditFile; currentAuditFile = tmpAuditFile;
close(); close();
// always append to an existing file (after restart) // always append to an existing file (after restart)
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(currentAuditFile, true), "UTF-8")); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(currentAuditFile, true), StandardCharsets.UTF_8));
writer.write("<jive xmlns=\"http://www.jivesoftware.org\">"); writer.write("<jive xmlns=\"http://www.jivesoftware.org\">");
xmlWriter = new org.jivesoftware.util.XMLWriter(writer); xmlWriter = new org.jivesoftware.util.XMLWriter(writer);
} }
......
...@@ -415,7 +415,7 @@ public class HttpBindServlet extends HttpServlet { ...@@ -415,7 +415,7 @@ public class HttpBindServlet extends HttpServlet {
byte b[] = new byte[1024]; byte b[] = new byte[1024];
int length; int length;
while (inputStream.isReady() && (length = inputStream.read(b)) != -1) { while (inputStream.isReady() && (length = inputStream.read(b)) != -1) {
buffer.append(new String(b, 0, length, "UTF-8" )); buffer.append(new String(b, 0, length, StandardCharsets.UTF_8));
} }
} }
......
...@@ -27,6 +27,7 @@ import java.io.Writer; ...@@ -27,6 +27,7 @@ import java.io.Writer;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
...@@ -65,11 +66,6 @@ public class SocketConnection implements Connection { ...@@ -65,11 +66,6 @@ public class SocketConnection implements Connection {
private static final Logger Log = LoggerFactory.getLogger(SocketConnection.class); private static final Logger Log = LoggerFactory.getLogger(SocketConnection.class);
/**
* The utf-8 charset for decoding and encoding XMPP packet streams.
*/
public static final String CHARSET = "UTF-8";
private static Map<SocketConnection, String> instances = private static Map<SocketConnection, String> instances =
new ConcurrentHashMap<SocketConnection, String>(); new ConcurrentHashMap<SocketConnection, String>();
...@@ -145,11 +141,11 @@ public class SocketConnection implements Connection { ...@@ -145,11 +141,11 @@ public class SocketConnection implements Connection {
// DANIELE: Modify socket to use channel // DANIELE: Modify socket to use channel
if (socket.getChannel() != null) { if (socket.getChannel() != null) {
writer = Channels.newWriter( writer = Channels.newWriter(
ServerTrafficCounter.wrapWritableChannel(socket.getChannel()), CHARSET); ServerTrafficCounter.wrapWritableChannel(socket.getChannel()), StandardCharsets.UTF_8.newEncoder(), -1);
} }
else { else {
writer = new BufferedWriter(new OutputStreamWriter( writer = new BufferedWriter(new OutputStreamWriter(
ServerTrafficCounter.wrapOutputStream(socket.getOutputStream()), CHARSET)); ServerTrafficCounter.wrapOutputStream(socket.getOutputStream()), StandardCharsets.UTF_8));
} }
this.backupDeliverer = backupDeliverer; this.backupDeliverer = backupDeliverer;
xmlSerializer = new XMLSocketWriter(writer, this); xmlSerializer = new XMLSocketWriter(writer, this);
...@@ -181,7 +177,7 @@ public class SocketConnection implements Connection { ...@@ -181,7 +177,7 @@ public class SocketConnection implements Connection {
// Start handshake // Start handshake
tlsStreamHandler.start(); tlsStreamHandler.start();
// Use new wrapped writers // Use new wrapped writers
writer = new BufferedWriter(new OutputStreamWriter(tlsStreamHandler.getOutputStream(), CHARSET)); writer = new BufferedWriter(new OutputStreamWriter(tlsStreamHandler.getOutputStream(), StandardCharsets.UTF_8));
xmlSerializer = new XMLSocketWriter(writer, this); xmlSerializer = new XMLSocketWriter(writer, this);
} }
} }
...@@ -199,13 +195,13 @@ public class SocketConnection implements Connection { ...@@ -199,13 +195,13 @@ public class SocketConnection implements Connection {
ServerTrafficCounter.wrapOutputStream(socket.getOutputStream()), ServerTrafficCounter.wrapOutputStream(socket.getOutputStream()),
JZlib.Z_BEST_COMPRESSION); JZlib.Z_BEST_COMPRESSION);
out.setFlushMode(JZlib.Z_PARTIAL_FLUSH); out.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET)); writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
xmlSerializer = new XMLSocketWriter(writer, this); xmlSerializer = new XMLSocketWriter(writer, this);
} }
else { else {
ZOutputStream out = new ZOutputStream(tlsStreamHandler.getOutputStream(), JZlib.Z_BEST_COMPRESSION); ZOutputStream out = new ZOutputStream(tlsStreamHandler.getOutputStream(), JZlib.Z_BEST_COMPRESSION);
out.setFlushMode(JZlib.Z_PARTIAL_FLUSH); out.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET)); writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
xmlSerializer = new XMLSocketWriter(writer, this); xmlSerializer = new XMLSocketWriter(writer, this);
} }
} catch (IOException e) { } catch (IOException e) {
......
...@@ -33,6 +33,8 @@ import org.xmlpull.v1.XmlPullParserException; ...@@ -33,6 +33,8 @@ import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmpp.packet.StreamError; import org.xmpp.packet.StreamError;
import java.nio.charset.StandardCharsets;
/** /**
* A ConnectionHandler is responsible for creating new sessions, destroying sessions and delivering * A ConnectionHandler is responsible for creating new sessions, destroying sessions and delivering
* received XML stanzas to the proper StanzaHandler. * received XML stanzas to the proper StanzaHandler.
...@@ -43,10 +45,6 @@ public abstract class ConnectionHandler extends IoHandlerAdapter { ...@@ -43,10 +45,6 @@ public abstract class ConnectionHandler extends IoHandlerAdapter {
private static final Logger Log = LoggerFactory.getLogger(ConnectionHandler.class); private static final Logger Log = LoggerFactory.getLogger(ConnectionHandler.class);
/**
* The utf-8 charset for decoding and encoding Jabber packet streams.
*/
static final String CHARSET = "UTF-8";
static final String XML_PARSER = "XML-PARSER"; static final String XML_PARSER = "XML-PARSER";
protected static final String HANDLER = "HANDLER"; protected static final String HANDLER = "HANDLER";
protected static final String CONNECTION = "CONNECTION"; protected static final String CONNECTION = "CONNECTION";
...@@ -84,7 +82,7 @@ public abstract class ConnectionHandler extends IoHandlerAdapter { ...@@ -84,7 +82,7 @@ public abstract class ConnectionHandler extends IoHandlerAdapter {
@Override @Override
public void sessionOpened(IoSession session) throws Exception { public void sessionOpened(IoSession session) throws Exception {
// Create a new XML parser for the new connection. The parser will be used by the XMPPDecoder filter. // Create a new XML parser for the new connection. The parser will be used by the XMPPDecoder filter.
final XMLLightweightParser parser = new XMLLightweightParser(CHARSET); final XMLLightweightParser parser = new XMLLightweightParser(StandardCharsets.UTF_8);
session.setAttribute(XML_PARSER, parser); session.setAttribute(XML_PARSER, parser);
// Create a new NIOConnection for the new session // Create a new NIOConnection for the new session
final NIOConnection connection = createNIOConnection(session); final NIOConnection connection = createNIOConnection(session);
......
...@@ -113,8 +113,8 @@ class XMLLightweightParser { ...@@ -113,8 +113,8 @@ class XMLLightweightParser {
PropertyEventDispatcher.addListener(new PropertyListener()); PropertyEventDispatcher.addListener(new PropertyListener());
} }
public XMLLightweightParser(String charset) { public XMLLightweightParser(Charset charset) {
encoder = Charset.forName(charset).newDecoder() encoder = charset.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE) .onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE); .onUnmappableCharacter(CodingErrorAction.REPLACE);
} }
......
...@@ -24,6 +24,7 @@ import java.io.IOException; ...@@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
...@@ -272,7 +273,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou ...@@ -272,7 +273,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou
XMPPPacketReader reader = new XMPPPacketReader(); XMPPPacketReader reader = new XMPPPacketReader();
reader.getXPPParser().setInput(new InputStreamReader(socket.getInputStream(), reader.getXPPParser().setInput(new InputStreamReader(socket.getInputStream(),
CHARSET)); StandardCharsets.UTF_8));
// Get the answer from the Receiving Server // Get the answer from the Receiving Server
XmlPullParser xpp = reader.getXPPParser(); XmlPullParser xpp = reader.getXPPParser();
for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) { for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) {
...@@ -401,7 +402,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou ...@@ -401,7 +402,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou
connection.deliverRawText(openingStream.toString()); connection.deliverRawText(openingStream.toString());
// Reset the parser to use the new secured reader // Reset the parser to use the new secured reader
xpp.setInput(new InputStreamReader(connection.getTLSStreamHandler().getInputStream(), CHARSET)); xpp.setInput(new InputStreamReader(connection.getTLSStreamHandler().getInputStream(), StandardCharsets.UTF_8));
// Skip new stream element // Skip new stream element
for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) { for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) {
eventType = xpp.next(); eventType = xpp.next();
...@@ -446,7 +447,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou ...@@ -446,7 +447,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou
ZInputStream in = new ZInputStream( ZInputStream in = new ZInputStream(
connection.getTLSStreamHandler().getInputStream()); connection.getTLSStreamHandler().getInputStream());
in.setFlushMode(JZlib.Z_PARTIAL_FLUSH); in.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
xpp.setInput(new InputStreamReader(in, CHARSET)); xpp.setInput(new InputStreamReader(in, StandardCharsets.UTF_8));
// Skip the opening stream sent by the server // Skip the opening stream sent by the server
for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;)
{ {
...@@ -551,7 +552,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou ...@@ -551,7 +552,7 @@ public class LocalOutgoingServerSession extends LocalServerSession implements Ou
// Reset the parser // Reset the parser
//xpp.resetInput(); //xpp.resetInput();
// // Reset the parser to use the new secured reader // // Reset the parser to use the new secured reader
xpp.setInput(new InputStreamReader(connection.getTLSStreamHandler().getInputStream(), CHARSET)); xpp.setInput(new InputStreamReader(connection.getTLSStreamHandler().getInputStream(), StandardCharsets.UTF_8));
// Skip the opening stream sent by the server // Skip the opening stream sent by the server
for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) { for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) {
eventType = xpp.next(); eventType = xpp.next();
......
...@@ -29,6 +29,7 @@ import java.io.InputStream; ...@@ -29,6 +29,7 @@ import java.io.InputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.StringReader; import java.io.StringReader;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
...@@ -670,7 +671,7 @@ public class UpdateManager extends BasicModule { ...@@ -670,7 +671,7 @@ public class UpdateManager extends BasicModule {
file.delete(); file.delete();
} }
// Create new version.xml with returned data // Create new version.xml with returned data
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) { try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
OutputFormat prettyPrinter = OutputFormat.createPrettyPrint(); OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter); XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
xmlWriter.write(xmlResponse); xmlWriter.write(xmlResponse);
...@@ -717,7 +718,7 @@ public class UpdateManager extends BasicModule { ...@@ -717,7 +718,7 @@ public class UpdateManager extends BasicModule {
file.delete(); file.delete();
} }
// Create new version.xml with returned data // Create new version.xml with returned data
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
OutputFormat prettyPrinter = OutputFormat.createPrettyPrint(); OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter); XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
xmlWriter.write(xml); xmlWriter.write(xml);
......
...@@ -35,6 +35,7 @@ import java.io.OutputStreamWriter; ...@@ -35,6 +35,7 @@ import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
...@@ -113,7 +114,7 @@ public class XMLProperties { ...@@ -113,7 +114,7 @@ public class XMLProperties {
* @throws IOException if an exception occurs when reading the stream. * @throws IOException if an exception occurs when reading the stream.
*/ */
public XMLProperties(InputStream in) throws IOException { public XMLProperties(InputStream in) throws IOException {
try (Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"))) { try (Reader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
buildDoc(reader); buildDoc(reader);
} }
} }
...@@ -745,7 +746,7 @@ public class XMLProperties { ...@@ -745,7 +746,7 @@ public class XMLProperties {
boolean error = false; boolean error = false;
// Write data out to a temporary file first. // Write data out to a temporary file first.
File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");; File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");;
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8"))) { try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), StandardCharsets.UTF_8))) {
OutputFormat prettyPrinter = OutputFormat.createPrettyPrint(); OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter); XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
xmlWriter.write(document); xmlWriter.write(document);
......
...@@ -8,6 +8,7 @@ import org.xml.sax.ext.LexicalHandler; ...@@ -8,6 +8,7 @@ import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
/** /**
...@@ -91,7 +92,7 @@ public class XMLWriter extends XMLFilterImpl implements LexicalHandler { ...@@ -91,7 +92,7 @@ public class XMLWriter extends XMLFilterImpl implements LexicalHandler {
public XMLWriter() throws UnsupportedEncodingException { public XMLWriter() throws UnsupportedEncodingException {
this.format = DEFAULT_FORMAT; this.format = DEFAULT_FORMAT;
this.writer = new BufferedWriter( new OutputStreamWriter( System.out, "UTF-8" ) ); this.writer = new BufferedWriter( new OutputStreamWriter( System.out, StandardCharsets.UTF_8) );
this.autoFlush = true; this.autoFlush = true;
namespaceStack.push(Namespace.NO_NAMESPACE); namespaceStack.push(Namespace.NO_NAMESPACE);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment