@Override public int read(final byte[] byteArray, final int off, final int len) throws IOException { if (bb == null) { throw new IOException("read on a closed InputStream"); } if (byteArray == null) { throw new NullPointerException(); } if (off < 0 || off > byteArray.length || len < 0 || off + len > byteArray.length || off + len < 0) { throw new IndexOutOfBoundsException(); } if (len == 0) { return 0; } int size = Math.min(remainingBytes(), len); if (size == 0) { return -1; } bb.readBytes(byteArray, off, size); return size; }
public static int bytesReadBytes(ActiveMQBuffer message, final byte[] value, final int length) { if (!message.readable()) { return -1; } int read = Math.min(length, message.readableBytes()); if (read != 0) { message.readBytes(value, 0, read); } return read; }
@Override public void saveToOutputStream(final OutputStream out) throws ActiveMQException { try { byte[] readBuffer = new byte[getBodySize()]; getBodyBuffer().readBytes(readBuffer); out.write(readBuffer); out.flush(); } catch (IOException e) { throw ActiveMQClientMessageBundle.BUNDLE.errorSavingBody(e); } }
@Override public void decodeRest(final ActiveMQBuffer buffer) { int size = buffer.readInt(); body = new byte[size]; buffer.readBytes(body); continues = buffer.readBoolean(); }
/** * Utility for reading bytes out of streaming. * It will return remainingBytes, bytesRead * * @param remainingBytes remaining Bytes from previous read. Send it to 0 if it was the first call for the message * @param buff * @return a pair of remaining bytes and bytes read */ public static Pair<Integer, Integer> streamReadBytes(ActiveMQBuffer buff, int remainingBytes, byte[] value) { if (remainingBytes == -1) { return new Pair<>(0, -1); } else if (remainingBytes == 0) { byte type = buff.readByte(); if (type != DataConstants.BYTES) { throw new IllegalStateException("Invalid conversion"); } remainingBytes = buff.readInt(); } int read = Math.min(value.length, remainingBytes); buff.readBytes(value, 0, read); remainingBytes -= read; if (remainingBytes == 0) { remainingBytes = -1; } return new Pair<>(remainingBytes, read); }
private ActiveMQBuffer inflate(ActiveMQBuffer buffer) throws DataFormatException { int bytesToRead = buffer.readableBytes(); Inflater inflater = new Inflater(); inflater.setInput(ByteUtil.getActiveArray(buffer.readBytes(bytesToRead).toByteBuffer())); //get the real size of large message long sizeBody = getLongProperty(Message.HDR_LARGE_BODY_SIZE); byte[] data = new byte[(int) sizeBody]; inflater.inflate(data); inflater.end(); ActiveMQBuffer qbuff = ActiveMQBuffers.wrappedBuffer(data); qbuff.resetReaderIndex(); qbuff.resetWriterIndex(); qbuff.writeBytes(data); buffer = qbuff; return buffer; }
@Override public void doBeforeReceive() throws ActiveMQException { super.doBeforeReceive(); try { int len = message.getBodyBuffer().readInt(); data = new byte[len]; message.getBodyBuffer().readBytes(data); } catch (Exception e) { data = null; } }
public static Object streamReadObject(ActiveMQBuffer buff) { byte type = buff.readByte(); switch (type) { case DataConstants.BOOLEAN: return buff.readBoolean(); case DataConstants.BYTE: return buff.readByte(); case DataConstants.SHORT: return buff.readShort(); case DataConstants.CHAR: return (char) buff.readShort(); case DataConstants.INT: return buff.readInt(); case DataConstants.LONG: return buff.readLong(); case DataConstants.FLOAT: return Float.intBitsToFloat(buff.readInt()); case DataConstants.DOUBLE: return Double.longBitsToDouble(buff.readLong()); case DataConstants.STRING: return buff.readNullableString(); case DataConstants.BYTES: int bufferLen = buff.readInt(); byte[] bytes = new byte[bufferLen]; buff.readBytes(bytes); return bytes; default: throw new IllegalStateException("Invalid conversion"); } }
bytes = buffer.borrowByteBuffer(size); index = 0; input.readBytes(bytes, 0, size);
final byte[] body = ByteUtil.getActiveArray(qbuff.readBytes(bytesToRead).toByteBuffer());
public static int bytesReadBytes(ActiveMQBuffer message, final byte[] value, final int length) { if (!message.readable()) { return -1; } int read = Math.min(length, message.readableBytes()); if (read != 0) { message.readBytes(value, 0, read); } return read; }
@Override public void saveToOutputStream(final OutputStream out) throws ActiveMQException { try { byte[] readBuffer = new byte[getBodySize()]; getBodyBuffer().readBytes(readBuffer); out.write(readBuffer); out.flush(); } catch (IOException e) { throw ActiveMQClientMessageBundle.BUNDLE.errorSavingBody(e); } }
@Override public void saveToOutputStream(final OutputStream out) throws ActiveMQException { try { byte[] readBuffer = new byte[getBodySize()]; getBodyBuffer().readBytes(readBuffer); out.write(readBuffer); out.flush(); } catch (IOException e) { throw ActiveMQClientMessageBundle.BUNDLE.errorSavingBody(e); } }
@Override public void decodeRest(final ActiveMQBuffer buffer) { journalID = buffer.readByte(); txId = buffer.readLong(); int size = buffer.readInt(); recordData = new byte[size]; buffer.readBytes(recordData); }
@Override public void doBeforeReceive() throws ActiveMQException { super.doBeforeReceive(); try { int len = message.getBodyBuffer().readInt(); data = new byte[len]; message.getBodyBuffer().readBytes(data); } catch (Exception e) { data = null; } }
@Override public void decode() throws Exception { super.decode(); ActiveMQBuffer buffer = getInnerMessage().getDataBuffer(); int size = buffer.readInt(); byte[] bytes = new byte[size]; buffer.readBytes(bytes); payload = new Binary(bytes); } }
@Test public void testBytesWithLength() throws Exception { byte[] bytes = RandomUtil.randomBytes(); // put only half of the bytes wrapper.writeBytes(bytes, 0, bytes.length / 2); byte[] b = new byte[bytes.length / 2]; wrapper.readBytes(b, 0, b.length); ActiveMQTestBase.assertEqualsByteArrays(b.length, bytes, b); }
private void checkBody(ClientMessage message, int bufferSize) { assertEquals(bufferSize, message.getBodySize()); byte[] body = new byte[message.getBodySize()]; message.getBodyBuffer().readBytes(body); for (int bpos = 0; bpos < bufferSize; bpos++) { if (getSamplebyte(bpos) != body[bpos]) { fail("body comparison failure at " + message); } } }