static String readString(ByteBuffer buffer) { int length = buffer.get() & 0xff; return new String(take(buffer, length), StandardCharsets.UTF_8); }
/** * Get a byte string from a ByteBuffer. * * @param buffer the buffer * @param length the number of bytes to get * @return the byte string */ public static ByteString getBytes(ByteBuffer buffer, int length) { return new ByteString(Buffers.take(buffer, length)); }
/** * Get a byte string from all remaining bytes of a ByteBuffer. * * @param buffer the buffer * @return the byte string */ public static ByteString getBytes(ByteBuffer buffer) { return new ByteString(Buffers.take(buffer)); }
/** * Evaluate a sasl challenge. If the result is non-{@code null} then the negotiation is not yet complete and the data * returned needs to be sent to the server as a response. If the result is {@code null} * then negotiation was successful and no response needs to be sent to the server. * <p> * The {@code source} buffer should have its position and remaining length set to encompass exactly one SASL * message. The SASL message itself does not encode any length information so it is up to the protocol implementer * to ensure that the message is properly framed. * * @param client the SASL client to use to evaluate the challenge message * @param source the source buffer from which the challenge message should be read * @return {@code null} if negotiation is complete and successful, or the response otherwise * @throws SaslException if negotiation failed or another error occurred */ public static byte[] evaluateChallenge(SaslClient client, ByteBuffer source) throws SaslException { return client.evaluateChallenge(Buffers.take(source)); }
/** * Evaluate a sasl response. If the result is non-{@code null} then the negotiation is not yet complete and the data * returned needs to be sent to the server as a response. If the result is {@code null} * then negotiation was successful and no response needs to be sent to the client (other than a successful completion * message, depending on the protocol). * <p> * The {@code source} buffer should have its position and remaining length set to encompass exactly one SASL * message. The SASL message itself does not encode any length information so it is up to the protocol implementer * to ensure that the message is properly framed. * * @param server the SASL server to use to evaluate the response message * @param source the source buffer from which the response message should be read * @return {@code true} if negotiation is complete and successful, {@code false} otherwise * @throws SaslException if negotiation failed or another error occurred */ public static byte[] evaluateResponse(final SaslServer server, final ByteBuffer source) throws SaslException { return server.evaluateResponse(source.hasRemaining() ? Buffers.take(source) : EMPTY_BYTES); }
/** * Take all of the remaining bytes from the buffers and return them in an array. * * @param buffers the buffer to read * @param offs the offset into the array * @param len the number of buffers * @return the bytes */ public static byte[] take(final ByteBuffer[] buffers, final int offs, final int len) { if (len == 1) return take(buffers[offs]); final long remaining = Buffers.remaining(buffers, offs, len); if (remaining == 0L) return NO_BYTES; if (remaining > Integer.MAX_VALUE) throw new OutOfMemoryError("Array too large"); final byte[] bytes = new byte[(int) remaining]; int o = 0; int rem; ByteBuffer buffer; for (int i = 0; i < len; i ++) { buffer = buffers[i + offs]; rem = buffer.remaining(); buffer.get(bytes, o, rem); o += rem; } return bytes; }
return; } else { byte[] tmp = take(source); try { target.write(tmp);
public boolean send(final ByteBuffer[] srcs, final int offs, final int len) throws IOException { if (! doSend()) { return false; } final ByteBuffer wrapped = ByteBuffer.wrap(wrapper.wrap(Buffers.take(srcs, offs, len))); if (! next.send(wrapped)) { this.buffer = wrapped; } return true; }
public boolean send(final ByteBuffer[] srcs, final int offs, final int len) throws IOException { if (! doSend()) { return false; } final byte[] bytes = Buffers.take(srcs, offs, len); final ByteBuffer wrapped = ByteBuffer.wrap(wrapper.unwrap(bytes)); if (! next.send(wrapped)) { this.buffer = wrapped; } return true; }
/** * Wrap a message. Wrapping occurs from the source buffer to the destination idea. * <p> * The {@code source} buffer should have its position and remaining length set to encompass exactly one SASL * message (without the length field). The SASL message itself does not encode any length information so it is up * to the protocol implementer to ensure that the message is properly framed. * * @param server the SASL server to wrap with * @param source the buffers from which bytes should be read * @return the wrap result * @throws SaslException if a SASL error occurs * @see SaslServer#wrap(byte[], int, int) */ public static byte[] wrap(final SaslServer server, final ByteBuffer source) throws SaslException { final byte[] result; final int len = source.remaining(); if (len == 0) { result = server.wrap(EMPTY_BYTES, 0, len); } else if (source.hasArray()) { final byte[] array = source.array(); final int offs = source.arrayOffset(); source.position(source.position() + len); result = server.wrap(array, offs, len); } else { result = server.wrap(Buffers.take(source, len), 0, len); } return result; }
/** * Unwrap a message. Unwrapping occurs from the source buffer to the destination idea. * <p> * The {@code source} buffer should have its position and remaining length set to encompass exactly one SASL * message (without the length field). The SASL message itself does not encode any length information so it is up * to the protocol implementer to ensure that the message is properly framed. * * @param client the SASL client to unwrap with * @param source the buffers from which bytes should be read * @return the wrap result * @throws SaslException if a SASL error occurs * @see SaslClient#unwrap(byte[], int, int) */ public static byte[] unwrap(final SaslClient client, final ByteBuffer source) throws SaslException { final byte[] result; final int len = source.remaining(); if (len == 0) { result = client.unwrap(EMPTY_BYTES, 0, len); } else if (source.hasArray()) { final byte[] array = source.array(); final int offs = source.arrayOffset(); source.position(source.position() + len); result = client.unwrap(array, offs, len); } else { result = client.unwrap(Buffers.take(source, len), 0, len); } return result; }
/** * Unwrap a message. Unwrapping occurs from the source buffer to the destination idea. * <p> * The {@code source} buffer should have its position and remaining length set to encompass exactly one SASL * message (without the length field). The SASL message itself does not encode any length information so it is up * to the protocol implementer to ensure that the message is properly framed. * * @param server the SASL server to unwrap with * @param source the buffers from which bytes should be read * @return the wrap result * @throws SaslException if a SASL error occurs * @see SaslServer#unwrap(byte[], int, int) */ public static byte[] unwrap(final SaslServer server, final ByteBuffer source) throws SaslException { final byte[] result; final int len = source.remaining(); if (len == 0) { result = server.unwrap(EMPTY_BYTES, 0, len); } else if (source.hasArray()) { final byte[] array = source.array(); final int offs = source.arrayOffset(); source.position(source.position() + len); result = server.unwrap(array, offs, len); } else { result = server.unwrap(Buffers.take(source, len), 0, len); } return result; }
private PooledByteBuffer toArrayBacked(ByteBuffer buffer, ByteBufferPool pool) { if(pool.getBufferSize() < buffer.remaining()) { return new ImmediatePooledByteBuffer(ByteBuffer.wrap(Buffers.take(buffer))); } PooledByteBuffer newBuf = pool.getArrayBackedPool().allocate(); newBuf.getBuffer().put(buffer); newBuf.getBuffer().flip(); return newBuf; }
/** * Wrap a message. Wrapping occurs from the source buffer to the destination idea. * <p> * The {@code source} buffer should have its position and remaining length set to encompass exactly one SASL * message (without the length field). The SASL message itself does not encode any length information so it is up * to the protocol implementer to ensure that the message is properly framed. * * @param client the SASL client to wrap with * @param source the buffers from which bytes should be read * @return the wrap result * @throws SaslException if a SASL error occurs * @see SaslClient#wrap(byte[], int, int) */ public static byte[] wrap(final SaslClient client, final ByteBuffer source) throws SaslException { final byte[] result; final int len = source.remaining(); if (len == 0) { result = client.wrap(EMPTY_BYTES, 0, len); } else if (source.hasArray()) { final byte[] array = source.array(); final int offs = source.arrayOffset(); source.position(source.position() + len); result = client.wrap(array, offs, len); } else { result = client.wrap(Buffers.take(source, len), 0, len); } return result; }
cnt += t; } else { final byte[] bytes = Buffers.take(src); deflater.setInput(bytes); c1 = deflater.getTotalIn();
cnt += t; } else { final byte[] bytes = Buffers.take(src); deflater.setInput(bytes); c1 = deflater.getTotalIn();
String reason = new String(Buffers.take(buffer), StandardCharsets.UTF_8); pendingChannel.getResult().setException(new ServiceOpenException(reason)); break;
final byte[] challenge = Buffers.take(buffer, buffer.remaining()); try { response = saslClient.evaluateChallenge(challenge); try { final boolean clientComplete = saslClient.isComplete(); final byte[] challenge = Buffers.take(buffer, buffer.remaining()); if (!clientComplete) try { final byte[] response = saslClient.evaluateChallenge(challenge);
/** * Get a byte string from a ByteBuffer. * * @param buffer the buffer * @param length the number of bytes to get * @return the byte string */ public static ByteString getBytes(ByteBuffer buffer, int length) { return new ByteString(Buffers.take(buffer, length)); }
private PooledByteBuffer toArrayBacked(ByteBuffer buffer, ByteBufferPool pool) { if(pool.getBufferSize() < buffer.remaining()) { return new ImmediatePooledByteBuffer(ByteBuffer.wrap(Buffers.take(buffer))); } PooledByteBuffer newBuf = pool.getArrayBackedPool().allocate(); newBuf.getBuffer().put(buffer); newBuf.getBuffer().flip(); return newBuf; }