org.eclipse.jetty.util.BufferUtil Java Examples

The following examples show how to use org.eclipse.jetty.util.BufferUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TlsOrPlainConnectionFactoryTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private int writeBytes(ByteBuffer dst, byte... toWrite)
{
    int written = 0;
    if (BufferUtil.space(dst) > 0)
    {
        final int pos = BufferUtil.flipToFill(dst);
        try
        {
            for (; written < toWrite.length; written++)
            {
                dst.put(toWrite[written]);
            }
        }
        finally
        {
            BufferUtil.flipToFlush(dst, pos);
        }
    }
    return written;
}
 
Example #2
Source File: BadSocket.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
  if (isNotConnected()) {
    return;
  }

  try {
    RemoteEndpoint remote = getRemote();
    remote.sendBytes(BufferUtil.toBuffer(payload, offset, len), null);
    if (remote.getBatchMode() == BatchMode.ON) {
      remote.flush();
    }
  } catch (IOException x) {
    throw new RuntimeIOException(x);
  }
}
 
Example #3
Source File: EchoSocket.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
  if (isNotConnected()) {
    return;
  }

  try {
    RemoteEndpoint remote = getRemote();
    remote.sendBytes(BufferUtil.toBuffer(payload, offset, len), null);
    if (remote.getBatchMode() == BatchMode.ON) {
      remote.flush();
    }
  } catch (IOException x) {
    throw new RuntimeIOException(x);
  }
}
 
Example #4
Source File: MarkableEndPoint.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private int fillFromPreserved(final ByteBuffer dst)
{
    int filled = 0;
    final int pos = BufferUtil.flipToFill(dst);
    try
    {
        Iterator<ByteBuffer> bufferIterator = _preserved.iterator();
        while (bufferIterator.hasNext())
        {
            ByteBuffer buffer = bufferIterator.next();
            final int bufRemaining = buffer.remaining();
            int dstRemaining = dst.remaining();
            if (dstRemaining >= bufRemaining)
            {
                dst.put(buffer);
            }
            else
            {
                ByteBuffer slice = buffer.slice();
                slice.limit(dstRemaining);
                dst.put(slice);
                buffer.position(buffer.position() + dstRemaining);
            }
            filled += bufRemaining - buffer.remaining();
            if (buffer.hasRemaining())
            {
                return filled;
            }
            bufferIterator.remove();
        }
    }
    finally
    {
        BufferUtil.flipToFlush(dst, pos);
    }
    return filled;
}
 
Example #5
Source File: MarkableEndPoint.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private ByteBuffer preserve(final ByteBuffer dst, final int newLimit, final int oldLimit)
{
    ByteBuffer buf = BufferUtil.allocate(newLimit - oldLimit);
    ByteBuffer slice = dst.slice();
    slice.position(oldLimit);
    slice.limit(newLimit);
    BufferUtil.append(buf, slice);
    return buf;
}
 
Example #6
Source File: TlsOrPlainConnectionFactoryTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnFillableForPlain() throws Exception
{
    AtomicBoolean firstPart = new AtomicBoolean(true);
    Answer<Object> answer = (InvocationOnMock invocation) ->
    {
        ByteBuffer dst =
                (ByteBuffer) invocation.getArguments()[0];
        if (firstPart.get())
        {
            firstPart.set(false);
            return writeBytes(dst,
                              "HTTP 1".getBytes());
        }
        return writeBytes(dst,
                          ".1\n\n".getBytes());
    };
    when(_endPoint.fill(any(ByteBuffer.class))).thenAnswer(answer);

    TlsOrPlainConnectionFactory.PlainOrTlsConnection connection = _factory.newConnection(_connector, _endPoint);

    connection.onFillable();

    verify(_actualConnection).onOpen();
    verify(_sslContextFactory, times(0)).newSSLEngine(any());
    verify(_endPoint).fill(any());

    ByteBuffer buffer = BufferUtil.allocate(4);
    int result = connection.getEndPoint().fill(buffer);
    assertEquals((long) 4, (long) result);

    assertEquals("HTTP", new String(buffer.array()));

    buffer = BufferUtil.allocate(6);
    result = connection.getEndPoint().fill(buffer);
    assertEquals((long) 6, (long) result);
    assertEquals(" 1.1\n\n", new String(buffer.array()));
    verify(_endPoint, times(2)).fill(any());
}
 
Example #7
Source File: TlsOrPlainConnectionFactoryTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnFillableForTLS() throws Exception
{
    AtomicBoolean firstPart = new AtomicBoolean(true);
    Answer<Object> answer = (InvocationOnMock invocation) ->
    {
        ByteBuffer dst =
                (ByteBuffer) invocation.getArguments()[0];
        if (firstPart.get())
        {
            firstPart.set(false);
            return writeBytes(dst,
                              (byte) 22,
                              (byte) 3,
                              (byte) 1);
        }
        return writeBytes(dst,
                          (byte) 0,
                          (byte) 0,
                          (byte) 1);
    };
    when(_endPoint.fill(any(ByteBuffer.class))).thenAnswer(answer);

    TlsOrPlainConnectionFactory.PlainOrTlsConnection connection = _factory.newConnection(_connector, _endPoint);

    connection.onFillable();

    verify(_endPoint).fillInterested(any(Callback.class));

    connection.onFillable();

    verify(_actualConnection).onOpen();
    verify(_sslContextFactory).newSSLEngine(any());

    ByteBuffer buffer = BufferUtil.allocate(4);
    int result = connection.getEndPoint().fill(buffer);
    assertEquals((long) 4, (long) result);

    assertTrue(Arrays.equals(new byte[]{(byte) 22, (byte) 3, (byte) 1, (byte) 0}, buffer.array()));
    buffer = BufferUtil.allocate(2);

    result = connection.getEndPoint().fill(buffer);
    assertEquals((long) 2, (long) result);
    assertTrue(Arrays.equals(new byte[]{(byte) 0, (byte) 1}, buffer.array()));
    verify(_endPoint, times(3)).fill(any());
}
 
Example #8
Source File: AsyncRestServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void onContent(Response response, ByteBuffer content) {
  byte[] bytes = BufferUtil.toArray(content);
  utf8Content.append(bytes, 0, bytes.length);
}