use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class StompFrame method decodeHeader.
/*
public int size() {
int rc = action.length() + 1;
if( headerList!=null ) {
for (HeaderEntry entry : headerList) {
rc += entry.getKey().length() + entry.getValue().length() + 2;
}
} else {
for (Map.Entry<Ascii,Ascii> entry : headerMap.entrySet()) {
rc += entry.getKey().length() + entry.getValue().length() + 2;
}
}
rc += content.length() + 3;
return rc;
}
*/
/*
public void write(DataOutput out, boolean includeBody) throws IOException {
write(out, action);
out.writeByte(NEWLINE_BYTE);
if( headerList!=null ) {
for (HeaderEntry entry : headerList) {
write(out, entry.getKey());
out.writeByte(COLON_BYTE);
write(out, entry.getValue());
out.writeByte(NEWLINE_BYTE);
}
} else {
for (Map.Entry<Ascii,Ascii> entry : headerMap.entrySet()) {
write(out, entry.getKey());
out.writeByte(COLON_BYTE);
write(out, entry.getValue());
out.writeByte(NEWLINE_BYTE);
}
}
//denotes end of headers with a new line
out.writeByte(NEWLINE_BYTE);
if (includeBody) {
write(out, content);
out.writeByte(NULL_BYTE);
out.writeByte(NEWLINE_BYTE);
}
}
*/
/*
public String toString() {
return toBuffer(false).ascii().toString();
}
public String errorMessage() {
Ascii value = getHeader(MESSAGE_HEADER);
if (value != null) {
return decodeHeader(value);
} else {
return contentAsString();
}
}
*/
public static String decodeHeader(Buffer value) {
if (value == null)
return null;
Buffer rc = new Buffer(value.length());
int pos = 0;
int max = value.length();
while (pos < max) {
if (startsWith(value, pos, ESCAPE_ESCAPE_SEQ.toBuffer())) {
rc.appendByte(ESCAPE_BYTE);
pos += 2;
} else if (startsWith(value, pos, COLON_ESCAPE_SEQ.toBuffer())) {
rc.appendByte(COLON_BYTE);
pos += 2;
} else if (startsWith(value, pos, NEWLINE_ESCAPE_SEQ.toBuffer())) {
rc.appendByte(NEWLINE_BYTE);
pos += 2;
} else {
rc.appendByte(value.getByte(pos));
pos += 1;
}
}
return rc.toString();
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class StompProtocolDecoder method read_binary_body.
private Action<StompFrame> read_binary_body(final StompFrame frame, final int contentLength) {
return new Action<StompFrame>() {
public StompFrame apply() throws IOException {
Buffer content = readBytes(contentLength + 1);
if (content != null) {
if (content.getByte(contentLength) != 0) {
throw new IOException("Expected null terminator after " + contentLength + " content bytes");
}
frame.content(chomp(content));
nextDecodeAction = read_action;
return frame;
} else {
return null;
}
}
};
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class SslSocketWrapper method handshake.
public void handshake() {
if (failed)
return;
try {
while (true) {
SSLEngineResult.HandshakeStatus status = engine.getHandshakeStatus();
switch(status) {
case FINISHED:
case NOT_HANDSHAKING:
return;
case NEED_TASK:
final Runnable task = engine.getDelegatedTask();
if (task != null) {
task.run();
}
break;
case NEED_WRAP:
if (plainWriteBuffer == null) {
plainWriteBuffer = new Buffer();
}
pumpWrites(false);
break;
case NEED_UNWRAP:
if (encryptedReadBuffer != null) {
pumpReads(false);
break;
} else {
return;
}
default:
System.err.println("Unexpected ssl engine handshake status: " + status);
break;
}
}
} finally {
SSLEngineResult.HandshakeStatus status = engine.getHandshakeStatus();
if (status == NOT_HANDSHAKING) {
pumpWrites(false);
pumpReads(false);
}
}
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class SslSocketWrapper method pumpReads.
private void pumpReads(boolean allowHandshake) {
boolean pump = true;
while (pump) {
pump = false;
if (readPaused > 0 || failed) {
return;
}
if (encryptedReadBuffer != null && plainReadBuffer == null && !encryptedReadBufferUnderflow) {
ByteBuffer input = ByteBuffer.wrap(encryptedReadBuffer.getBytes());
ByteBuffer output = ByteBuffer.allocate(engine.getSession().getApplicationBufferSize());
try {
boolean done = false;
while (!done) {
done = true;
SSLEngineResult result = engine.unwrap(input, output);
switch(result.getStatus()) {
case CLOSED:
engine.closeInbound();
break;
case BUFFER_UNDERFLOW:
encryptedReadBufferUnderflow = true;
break;
case OK:
switch(engine.getHandshakeStatus()) {
case NEED_TASK:
case NEED_WRAP:
break;
default:
done = !input.hasRemaining();
}
break;
case BUFFER_OVERFLOW:
throw new SSLException("BUFFER_OVERFLOW");
}
// Lets fill the plain buffer..
output.flip();
if (output.remaining() > 0) {
pump = true;
int len = output.remaining();
if (plainReadBuffer == null) {
plainReadBuffer = new Buffer(len);
}
plainReadBuffer.appendBytes(output.array(), output.arrayOffset() + output.position(), len);
}
output.clear();
}
} catch (SSLException e) {
onFailure(e);
return;
} finally {
int len = input.remaining();
if (len > 0) {
// we need to compact the encryptedReadBuffer
if (input.position() != 0) {
encryptedReadBuffer = new Buffer(len);
encryptedReadBuffer.appendBytes(input.array(), input.arrayOffset() + input.position(), len);
}
} else {
// everything was consumed.
encryptedReadBuffer = null;
}
}
}
// Send the plain buffer to the the data handler...
if (plainReadBuffer != null && readPaused == 0) {
pump = true;
Buffer data = plainReadBuffer;
plainReadBuffer = null;
Handler<Buffer> handler = plainDataHandler;
if (handler != null) {
handler.handle(data);
}
}
if (encryptedReadBuffer == null && plainReadBuffer == null && encryptedReadEOF) {
encryptedReadEOF = false;
Handler<Void> handler = plainEndHandler;
if (handler != null) {
handler.handle(null);
}
}
if (engine.getHandshakeStatus() != NOT_HANDSHAKING) {
if (allowHandshake) {
handshake();
}
return;
}
}
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class Ascii method encode.
public static Buffer encode(String value) {
int size = value.length();
Buffer rc = new Buffer(size);
for (int i = 0; i < size; i++) {
rc.appendByte((byte) (value.charAt(i) & 0xFF));
}
return rc;
}
Aggregations