SAM: Use SAMHandler.writeString() where possible,

Use DataHelper.getASCII() for byte conversion
This commit is contained in:
zzz
2014-06-27 16:46:57 +00:00
parent 7888705b01
commit 38a4728283
5 changed files with 16 additions and 29 deletions

View File

@ -519,13 +519,8 @@ public class SAMBridge implements Runnable, ClientApp {
} catch (SAMException e) {
if (_log.shouldLog(Log.ERROR))
_log.error("SAM error: " + e.getMessage(), e);
try {
String reply = "HELLO REPLY RESULT=I2P_ERROR MESSAGE=\"" + e.getMessage() + "\"\n";
s.write(ByteBuffer.wrap(reply.getBytes("ISO-8859-1")));
} catch (IOException ioe) {
if (_log.shouldLog(Log.ERROR))
_log.error("SAM Error sending error reply", ioe);
}
String reply = "HELLO REPLY RESULT=I2P_ERROR MESSAGE=\"" + e.getMessage() + "\"\n";
SAMHandler.writeString(reply, s);
try { s.close(); } catch (IOException ioe) {}
} catch (Exception ee) {
try { s.close(); } catch (IOException ioe) {}

View File

@ -13,6 +13,7 @@ import java.nio.channels.SocketChannel;
import java.nio.ByteBuffer;
import java.util.Properties;
import net.i2p.data.DataHelper;
import net.i2p.util.I2PAppThread;
import net.i2p.util.Log;
@ -132,10 +133,11 @@ abstract class SAMHandler implements Runnable {
}
}
/** @return success */
public static boolean writeString(String str, SocketChannel out)
{
try {
writeBytes(ByteBuffer.wrap(str.getBytes("ISO-8859-1")), out);
writeBytes(ByteBuffer.wrap(DataHelper.getASCII(str)), out);
} catch (IOException e) {
//_log.debug("Caught IOException", e);
return false;

View File

@ -84,23 +84,13 @@ class SAMHandlerFactory {
String ver = chooseBestVersion(minVer, maxVer);
try {
if (ver == null) {
s.write(ByteBuffer.wrap(("HELLO REPLY RESULT=NOVERSION\n").getBytes("ISO-8859-1")));
return null ;
}
// Let's answer positively
s.write(ByteBuffer.wrap(("HELLO REPLY RESULT=OK VERSION="
+ ver + "\n").getBytes("ISO-8859-1")));
} catch (UnsupportedEncodingException e) {
log.error("Caught UnsupportedEncodingException ("
+ e.getMessage() + ")");
throw new SAMException("Character encoding error: "
+ e.getMessage());
} catch (IOException e) {
throw new SAMException("Error writing to socket: "
+ e.getMessage());
if (ver == null) {
SAMHandler.writeString("HELLO REPLY RESULT=NOVERSION\n", s);
return null;
}
// Let's answer positively
if (!SAMHandler.writeString("HELLO REPLY RESULT=OK VERSION=" + ver + "\n", s))
throw new SAMException("Error writing to socket");
// ...and instantiate the right SAM handler
int verMajor = getMajor(ver);

View File

@ -782,9 +782,8 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
ByteArrayOutputStream msg = new ByteArrayOutputStream();
String msgText = "RAW RECEIVED SIZE=" + data.length + "\n";
msg.write(msgText.getBytes("ISO-8859-1"));
msg.write(DataHelper.getASCII(msgText));
msg.write(data);
msg.flush();
if (_log.shouldLog(Log.DEBUG))
_log.debug("sending to client: " + msgText);
@ -820,7 +819,7 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
String msgText = "DATAGRAM RECEIVED DESTINATION=" + sender.toBase64()
+ " SIZE=" + data.length + "\n";
msg.write(msgText.getBytes("ISO-8859-1"));
msg.write(DataHelper.getASCII(msgText));
if (_log.shouldLog(Log.DEBUG))
_log.debug("sending to client: " + msgText);
@ -926,7 +925,7 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
if (_log.shouldLog(Log.DEBUG))
_log.debug("sending to client: " + msgText);
ByteBuffer prefix = ByteBuffer.wrap(msgText.getBytes("ISO-8859-1"));
ByteBuffer prefix = ByteBuffer.wrap(DataHelper.getASCII(msgText));
Object writeLock = getWriteLock();
synchronized (writeLock) {

View File

@ -10,6 +10,7 @@ import java.util.Properties;
import net.i2p.client.I2PSessionException;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
import net.i2p.util.Log;
@ -76,7 +77,7 @@ class SAMv3DatagramSession extends SAMDatagramSession implements SAMv3Handler.Se
} else {
String msg = sender.toBase64()+"\n";
ByteBuffer msgBuf = ByteBuffer.allocate(msg.length()+data.length);
msgBuf.put(msg.getBytes("ISO-8859-1"));
msgBuf.put(DataHelper.getASCII(msg));
msgBuf.put(data);
msgBuf.flip();
this.server.send(this.clientAddress, msgBuf);