forked from I2P_Developers/i2p.i2p
Susimail, SAM: More defensive ByteBuffer casting to avoid runtime issues (ticket #2775)
This commit is contained in:
@@ -13,6 +13,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.net.ConnectException;
|
||||
@@ -692,14 +693,15 @@ class SAMStreamSession implements SAMMessageSess {
|
||||
InputStream in = i2pSocket.getInputStream();
|
||||
|
||||
while (stillRunning) {
|
||||
data.clear();
|
||||
((Buffer)data).clear();
|
||||
read = Channels.newChannel(in).read(data);
|
||||
if (read == -1) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Handler " + id + ": connection closed");
|
||||
break;
|
||||
}
|
||||
data.flip();
|
||||
// not ByteBuffer to avoid Java 8/9 issues with flip()
|
||||
((Buffer)data).flip();
|
||||
recv.receiveStreamBytes(id, data);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.io.InputStream;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.net.ConnectException;
|
||||
import java.net.NoRouteToHostException;
|
||||
@@ -511,7 +512,8 @@ class SAMv2StreamSession extends SAMStreamSession
|
||||
break ;
|
||||
}
|
||||
|
||||
data.clear();
|
||||
// not ByteBuffer to avoid Java 8/9 issues
|
||||
((Buffer)data).clear();
|
||||
read = Channels.newChannel(in).read ( data );
|
||||
|
||||
if ( read == -1 )
|
||||
@@ -522,7 +524,8 @@ class SAMv2StreamSession extends SAMStreamSession
|
||||
}
|
||||
|
||||
totalReceived += read ;
|
||||
data.flip();
|
||||
// not ByteBuffer to avoid Java 8/9 issues with flip()
|
||||
((Buffer)data).flip();
|
||||
recv.receiveStreamBytes ( id, data );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.util.Properties;
|
||||
@@ -114,16 +115,17 @@ class SAMv3DatagramServer implements Handler {
|
||||
|
||||
while (!Thread.interrupted())
|
||||
{
|
||||
inBuf.clear();
|
||||
// not ByteBuffer to avoid Java 8/9 issues
|
||||
((Buffer)inBuf).clear();
|
||||
try {
|
||||
server.receive(inBuf);
|
||||
} catch (IOException e) {
|
||||
break ;
|
||||
}
|
||||
inBuf.flip();
|
||||
((Buffer)inBuf).flip();
|
||||
ByteBuffer outBuf = ByteBuffer.wrap(new byte[inBuf.remaining()]);
|
||||
outBuf.put(inBuf);
|
||||
outBuf.flip();
|
||||
((Buffer)outBuf).flip();
|
||||
// A new thread for every message is wildly inefficient...
|
||||
//new I2PAppThread(new MessageDispatcher(outBuf.array()), "MessageDispatcher").start();
|
||||
// inline
|
||||
|
||||
@@ -8,6 +8,7 @@ package net.i2p.sam;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress ;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -97,7 +98,8 @@ class SAMv3DatagramSession extends SAMDatagramSession implements Session, SAMDat
|
||||
ByteBuffer msgBuf = ByteBuffer.allocate(msg.length()+data.length);
|
||||
msgBuf.put(DataHelper.getASCII(msg));
|
||||
msgBuf.put(data);
|
||||
msgBuf.flip();
|
||||
// not ByteBuffer to avoid Java 8/9 issues with flip()
|
||||
((Buffer)msgBuf).flip();
|
||||
this.server.send(this.clientAddress, msgBuf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -121,7 +122,7 @@ class SAMv3RawSession extends SAMRawSession implements Session, SAMRawReceiver {
|
||||
msgBuf = ByteBuffer.allocate(data.length);
|
||||
}
|
||||
msgBuf.put(data);
|
||||
msgBuf.flip();
|
||||
((Buffer)msgBuf).flip();
|
||||
this.server.send(this.clientAddress, msgBuf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,10 +14,11 @@ import java.net.ConnectException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NoRouteToHostException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Properties;
|
||||
@@ -464,7 +465,8 @@ class SAMv3StreamSession extends SAMStreamSession implements Session
|
||||
bridge.register(this);
|
||||
try {
|
||||
while (!Thread.interrupted() && (in.read(buf)>=0 || buf.position() != 0)) {
|
||||
buf.flip();
|
||||
// not ByteBuffer to avoid Java 8/9 issues with flip()
|
||||
((Buffer)buf).flip();
|
||||
out.write(buf);
|
||||
buf.compact();
|
||||
}
|
||||
@@ -475,7 +477,7 @@ class SAMv3StreamSession extends SAMStreamSession implements Session
|
||||
in.close();
|
||||
} catch (IOException e) {}
|
||||
try {
|
||||
buf.flip();
|
||||
((Buffer)buf).flip();
|
||||
while (buf.hasRemaining()) {
|
||||
out.write(buf);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.i2p.sam;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -60,8 +61,8 @@ public class UTF8Reader extends Reader {
|
||||
_cb = CharBuffer.allocate(1);
|
||||
_dc = Charset.forName("UTF-8").newDecoder();
|
||||
} else {
|
||||
_bb.clear();
|
||||
_cb.clear();
|
||||
((Buffer)_bb).clear();
|
||||
((Buffer)_cb).clear();
|
||||
}
|
||||
_bb.put((byte) b);
|
||||
int end; // how many more
|
||||
@@ -88,12 +89,13 @@ public class UTF8Reader extends Reader {
|
||||
_bb.put((byte) b);
|
||||
}
|
||||
_dc.reset();
|
||||
_bb.flip();
|
||||
// not ByteBuffer to avoid Java 8/9 issues with flip()
|
||||
((Buffer)_bb).flip();
|
||||
CoderResult result = _dc.decode(_bb, _cb, true);
|
||||
// Overflow and underflow are not errors.
|
||||
// It seems to return underflow every time.
|
||||
// So just check if we got a character back in the buffer.
|
||||
_cb.flip();
|
||||
((Buffer)_cb).flip();
|
||||
if (result.isError() || !_cb.hasRemaining())
|
||||
return REPLACEMENT;
|
||||
// let underflow and overflow go, return first
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -65,7 +66,8 @@ public class DecodingOutputStream extends OutputStream {
|
||||
}
|
||||
|
||||
private void decodeAndWrite(boolean endOfInput) throws IOException {
|
||||
_bb.flip();
|
||||
// not ByteBuffer to avoid Java 8/9 issues with flip()
|
||||
((Buffer)_bb).flip();
|
||||
if (!_bb.hasRemaining())
|
||||
return;
|
||||
CoderResult result;
|
||||
@@ -83,9 +85,9 @@ public class DecodingOutputStream extends OutputStream {
|
||||
if (result == null || (result.isError() && !_cb.hasRemaining())) {
|
||||
_out.write(REPLACEMENT);
|
||||
} else {
|
||||
_cb.flip();
|
||||
((Buffer)_cb).flip();
|
||||
_out.append(_cb);
|
||||
_cb.clear();
|
||||
((Buffer)_cb).clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user