Java Code Examples for io.vertx.core.buffer.Buffer#getByteBuf()

The following examples show how to use io.vertx.core.buffer.Buffer#getByteBuf() . 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: ProtonTransport.java    From vertx-proton with Apache License 2.0 6 votes vote down vote up
private void pumpInbound(Buffer buffer) {
  if (failed) {
    if (LOG.isTraceEnabled()) {
      LOG.trace("Skipping processing of data following transport error: " + buffer);
    }
    return;
  }

  // Lets push bytes from vert.x to proton engine.
  try {
    ByteBuf data = buffer.getByteBuf();
    do {
      ByteBuffer transportBuffer = transport.tail();

      int amount = Math.min(transportBuffer.remaining(), data.readableBytes());
      transportBuffer.limit(transportBuffer.position() + amount);
      data.readBytes(transportBuffer);

      transport.process();
    } while (data.isReadable());
  } catch (Exception te) {
    failed = true;
    LOG.trace("Exception while processing transport input", te);
  }
}
 
Example 2
Source File: AsyncFileWriterImpl.java    From sfs with Apache License 2.0 6 votes vote down vote up
private AsyncFileWriterImpl doWrite(Buffer buffer, long position, Handler<AsyncResult<Void>> handler) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkArgument(position >= 0, "position must be >= 0");
    Handler<AsyncResult<Void>> wrapped = ar -> {
        if (ar.succeeded()) {
            if (handler != null) {
                handler.handle(ar);
            }
        } else {
            if (handler != null) {
                handler.handle(ar);
            } else {
                handleException(ar.cause());
            }
        }
    };
    ByteBuf buf = buffer.getByteBuf();
    if (buf.nioBufferCount() > 1) {
        doWrite(buf.nioBuffers(), position, wrapped);
    } else {
        ByteBuffer bb = buf.nioBuffer();
        doWrite(bb, position, bb.limit(), wrapped);
    }
    return this;
}
 
Example 3
Source File: TestStandardHttpServletRequestEx.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterMap_merge() throws IOException {
  Map<String, String[]> inherited = new HashMap<>();
  String[] v1 = new String[] {"v1-1", "v1-2"};
  inherited.put("p1", v1);

  Buffer buffer = Buffer.buffer("p1=v1-3;p2=v2");
  BufferInputStream inputStream = new BufferInputStream(buffer.getByteBuf());
  new Expectations() {
    {
      request.getParameterMap();
      result = inherited;
      request.getMethod();
      result = HttpMethod.PUT;
      request.getContentType();
      result = MediaType.APPLICATION_FORM_URLENCODED.toUpperCase(Locale.US) + ";abc";
      request.getInputStream();
      result = inputStream;
    }
  };

  Assert.assertThat(Collections.list(requestEx.getParameterNames()), Matchers.contains("p1", "p2"));
  Assert.assertThat(requestEx.getParameterValues("p1"), Matchers.arrayContaining("v1-1", "v1-2", "v1-3"));
  Assert.assertEquals("v1-1", requestEx.getParameter("p1"));
}
 
Example 4
Source File: VertxBufferImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public int readFromBuffer(int pos, Buffer buffer) {
  int len = buffer.getInt(pos);
  Buffer b = buffer.getBuffer(pos + 4, pos + 4 + len);
  this.buffer = b.getByteBuf();
  return pos + 4 + len;
}
 
Example 5
Source File: VertxHttpExchange.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf readBlocking() throws IOException {
    long readStart = System.currentTimeMillis();
    synchronized (request.connection()) {

        while (input1 == null && !eof && readError == null) {
            try {
                waitingForRead = true;
                long toWait = readTimeout - (System.currentTimeMillis() - readStart);
                if (toWait <= 0) {
                    throw new IOException("Read timeout");
                }
                request.connection().wait(toWait);
            } catch (InterruptedException e) {
                throw new InterruptedIOException(e.getMessage());
            } finally {
                waitingForRead = false;
            }
        }
        if (readError != null) {
            terminateRequest();
            throw new IOException(readError);
        }
        Buffer ret = input1;
        input1 = null;
        if (inputOverflow != null) {
            input1 = inputOverflow.poll();
            if (input1 == null) {
                request.fetch(1);
            }
        } else {
            request.fetch(1);
        }

        if (ret == null) {
            terminateRequest();
        }
        return ret == null ? null : ret.getByteBuf();
    }
}
 
Example 6
Source File: VertxBufferImpl.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public int readFromBuffer(int pos, Buffer buffer) {
    int len = buffer.getInt(pos);
    Buffer b = buffer.getBuffer(pos + 4, pos + 4 + len);
    this.buffer = b.getByteBuf();
    return pos + 4 + len;
}
 
Example 7
Source File: ProduceProcessor.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
default Object decodeResponse(Buffer buffer, JavaType type) throws Exception {
  if (buffer.length() == 0) {
    return null;
  }

  try (BufferInputStream input = new BufferInputStream(buffer.getByteBuf())) {
    return doDecodeResponse(input, type);
  }
}
 
Example 8
Source File: TcpParser.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected void onParse(Buffer buffer) {
  switch (status) {
    case TCP_HEADER:
      ByteBuf buf = buffer.getByteBuf();
      if (!firstNEqual(TCP_MAGIC, buf.array(), TCP_MAGIC.length)) {
        reset();
        return;
      }

      buf.skipBytes(TCP_MAGIC.length);
      msgId = buf.readLong();
      totalLen = buf.readInt();
      headerLen = buf.readInt();
      if (headerLen > totalLen || headerLen <= 0 || totalLen > TCP_MAX_REQUEST_LENGTH) {
        throw new IllegalStateException("possibly attack.");
      }

      if (totalLen == 0) {
        onReadOnePackage(null, null);
        return;
      }

      parser.fixedSizeMode(totalLen);
      status = ParseStatus.TCP_PAYLOAD;
      break;

    case TCP_PAYLOAD:
      Buffer headerBuffer = buffer.slice(0, headerLen);
      Buffer bodyBuffer = buffer.slice(headerLen, buffer.length());
      onReadOnePackage(headerBuffer, bodyBuffer);
      break;

    default:
      break;
  }
}
 
Example 9
Source File: VertxInputStream.java    From quarkus with Apache License 2.0 4 votes vote down vote up
protected ByteBuf readBlocking() throws IOException {
    long expire = System.currentTimeMillis() + timeout;
    synchronized (request.connection()) {
        while (input1 == null && !eof && readException == null) {
            long rem = expire - System.currentTimeMillis();
            if (rem <= 0) {
                //everything is broken, if read has timed out we can assume that the underling connection
                //is wrecked, so just close it
                request.connection().close();
                IOException throwable = new IOException("Read timed out");
                readException = throwable;
                throw throwable;
            }

            try {
                if (Context.isOnEventLoopThread()) {
                    throw new IOException("Attempting a blocking read on io thread");
                }
                waiting = true;
                request.connection().wait(rem);
            } catch (InterruptedException e) {
                throw new InterruptedIOException(e.getMessage());
            } finally {
                waiting = false;
            }
        }
        if (readException != null) {
            throw new IOException(readException);
        }
        Buffer ret = input1;
        input1 = null;
        if (inputOverflow != null) {
            input1 = inputOverflow.poll();
            if (input1 == null) {
                request.fetch(1);
            }
        } else if (!eof) {
            request.fetch(1);
        }
        return ret == null ? null : ret.getByteBuf();
    }
}
 
Example 10
Source File: MQTTEncoder.java    From vertx-mqtt-broker with Apache License 2.0 4 votes vote down vote up
public Buffer enc(AbstractMessage msg) throws Exception {
    Buffer buff = Buffer.buffer(2+msg.getRemainingLength());
    ByteBuf bb = buff.getByteBuf();
    encode(msg, bb);
    return Buffer.buffer(bb);
}
 
Example 11
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private static void binaryEncodeBYTEA(Buffer value, ByteBuf buff) {
  ByteBuf byteBuf = value.getByteBuf();
  buff.writeBytes(byteBuf);
}
 
Example 12
Source File: VertxUtils.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public static byte[] getBytesFast(Buffer buffer) {
  ByteBuf byteBuf = buffer.getByteBuf();
  return getBytesFast(byteBuf);
}
 
Example 13
Source File: VertxBufferImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public Buffer appendBuffer(Buffer buff, int offset, int len) {
  ByteBuf byteBuf = buff.getByteBuf();
  int from = byteBuf.readerIndex() + offset;
  buffer.writeBytes(byteBuf, from, len);
  return this;
}
 
Example 14
Source File: VertxBufferImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public Buffer setBuffer(int pos, Buffer b, int offset, int len) {
    ensureWritable(pos, len);
    ByteBuf byteBuf = b.getByteBuf();
    buffer.setBytes(pos, byteBuf, byteBuf.readerIndex() + offset, len);
    return this;
}
 
Example 15
Source File: VertxBufferImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public Buffer appendBuffer(Buffer buff, int offset, int len) {
    ByteBuf byteBuf = buff.getByteBuf();
    int from = byteBuf.readerIndex() + offset;
    buffer.writeBytes(byteBuf, from, len);
    return this;
}
 
Example 16
Source File: BufferConverter.java    From vertx-spring-boot with Apache License 2.0 4 votes vote down vote up
public DataBuffer toDataBuffer(Buffer buffer) {
    ByteBuf byteBuf = buffer.getByteBuf();
    return dataBufferFactory.wrap(byteBuf);
}
 
Example 17
Source File: VertxBufferImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public Buffer setBuffer(int pos, Buffer b, int offset, int len) {
  ensureWritable(pos, len);
  ByteBuf byteBuf = b.getByteBuf();
  buffer.setBytes(pos, byteBuf, byteBuf.readerIndex() + offset, len);
  return this;
}