Refine search
public CoreMessage initBuffer(final int initialMessageBufferSize) { buffer = ActiveMQBuffers.dynamicBuffer(initialMessageBufferSize).byteBuf(); // There's a bug in netty which means a dynamic buffer won't resize until you write a byte buffer.writeByte((byte) 0); buffer.setIndex(BODY_OFFSET, BODY_OFFSET); return this; }
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; }
public static ActiveMQBuffer randomBuffer(final int size, final long... data) { ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(size + 8 * data.length); for (long d : data) { buffer.writeLong(d); } for (int i = 0; i < size; i++) { buffer.writeByte(randomByte()); } return buffer; }
public ActiveMQBuffer toActiveMQBuffer() throws Exception { if (buffer == null) { if (isPing()) { buffer = ActiveMQBuffers.fixedBuffer(1); buffer.writeByte((byte) 10); size = buffer.writerIndex(); return buffer; int bodyLength = (bytesBody == null) ? 0 : bytesBody.length; buffer = ActiveMQBuffers.fixedBuffer(headBytes.length + bodyLength + END_OF_FRAME.length); buffer.writeBytes(headBytes); if (bytesBody != null) { buffer.writeBytes(bytesBody);
/** * Creates a <em>self-expanding</em> ActiveMQBuffer filled with the given byte array * * @param bytes the created buffer will be initially filled with this byte array * @return a self-expanding ActiveMQBuffer filled with the given byte array */ public static ActiveMQBuffer dynamicBuffer(final byte[] bytes) { ActiveMQBuffer buff = dynamicBuffer(bytes.length); buff.writeBytes(bytes); return buff; }
ActiveMQBuffer filesToRename = ActiveMQBuffers.dynamicBuffer(1); filesToRename.writeInt(0); } else { filesToRename.writeInt(files.size()); filesToRename.writeUTF(file.getFile().getFileName()); JournalInternalRecord controlRecord = new JournalAddRecord(true, 1, (byte) 0, EncoderPersister.getInstance(), new ByteArrayEncoding(filesToRename.toByteBuffer().array())); ActiveMQBuffer renameBuffer = ActiveMQBuffers.dynamicBuffer(filesToRename.writerIndex());
@Override public synchronized void broadcastConnectors() throws Exception { ActiveMQBuffer buff = ActiveMQBuffers.dynamicBuffer(4096); buff.writeString(nodeManager.getNodeId().toString()); buff.writeString(uniqueID); buff.writeInt(connectors.size()); for (TransportConfiguration tcConfig : connectors) { tcConfig.encode(buff); } // Only send as many bytes as we need. byte[] data = new byte[buff.readableBytes()]; buff.getBytes(buff.readerIndex(), data); endpoint.broadcast(data); }
@Override public void decodeRest(ActiveMQBuffer buffer) { super.decodeRest(buffer); handler = buffer.readSimpleString(); voteBuffer = ActiveMQBuffers.fixedBuffer(buffer.readableBytes()); buffer.readBytes(voteBuffer); }
@Test public void testReadData() throws Exception { ActiveMQBuffer dynamic = ActiveMQBuffers.dynamicBuffer(1); String str1 = RandomUtil.randomString(); String str2 = RandomUtil.randomString(); double d1 = RandomUtil.randomDouble(); float f1 = RandomUtil.randomFloat(); dynamic.writeUTF(str1); dynamic.writeString(str2); dynamic.writeDouble(d1); dynamic.writeFloat(f1); LargeMessageControllerImpl readBuffer = splitBuffer(3, dynamic.toByteBuffer().array()); Assert.assertEquals(str1, readBuffer.readUTF()); Assert.assertEquals(str2, readBuffer.readString()); Assert.assertEquals(d1, readBuffer.readDouble(), 0.000001); Assert.assertEquals(f1, readBuffer.readFloat(), 0.000001); }
@Override public int encode(final ByteBuffer bufferRead) throws ActiveMQException { ActiveMQBuffer buffer1 = ActiveMQBuffers.wrappedBuffer(bufferRead); return encode(buffer1, bufferRead.capacity()); }
@Test public void testTransportConfigurations() throws Exception { List<Pair<TransportConfiguration, TransportConfiguration>> connectorConfigs = new ArrayList<>(); Map<String, Object> liveParams = new HashMap<>(); liveParams.put(TransportConstants.PORT_PROP_NAME, 5665); TransportConfiguration live1 = new TransportConfiguration(NettyConnectorFactory.class.getName(), liveParams); Map<String, Object> backupParams = new HashMap<>(); backupParams.put(TransportConstants.PORT_PROP_NAME, 5775); TransportConfiguration backup1 = new TransportConfiguration(NettyConnectorFactory.class.getName(), backupParams); Map<String, Object> liveParams2 = new HashMap<>(); liveParams2.put(TransportConstants.PORT_PROP_NAME, 6665); TransportConfiguration live2 = new TransportConfiguration(NettyConnectorFactory.class.getName(), liveParams2); connectorConfigs.add(new Pair<>(live1, backup1)); connectorConfigs.add(new Pair<TransportConfiguration, TransportConfiguration>(live2, null)); ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(TransportConfigurationEncodingSupport.getEncodeSize(connectorConfigs)); TransportConfigurationEncodingSupport.encodeConfigs(buffer, connectorConfigs); assertEquals(buffer.capacity(), buffer.writerIndex()); buffer.readerIndex(0); List<Pair<TransportConfiguration, TransportConfiguration>> decodedConfigs = TransportConfigurationEncodingSupport.decodeConfigs(buffer); assertNotNull(decodedConfigs); assertEquals(2, decodedConfigs.size()); assertEquivalent(connectorConfigs.get(0).getA(), decodedConfigs.get(0).getA()); assertEquivalent(connectorConfigs.get(0).getB(), decodedConfigs.get(0).getB()); assertEquivalent(connectorConfigs.get(1).getA(), decodedConfigs.get(1).getA()); assertNull(decodedConfigs.get(1).getB()); }
private static void sendBadData(BroadcastEndpointFactory factoryEndpoint) throws Exception { BroadcastEndpoint endpoint = factoryEndpoint.createBroadcastEndpoint(); ActiveMQBuffer buffer = ActiveMQBuffers.dynamicBuffer(500); buffer.writeString("This is a test1!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); buffer.writeString("This is a test2!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); byte[] bytes = new byte[buffer.writerIndex()]; buffer.readBytes(bytes); // messing up with the string!!! for (int i = bytes.length - 10; i < bytes.length; i++) { bytes[i] = 0; } endpoint.openBroadcaster(); endpoint.broadcast(bytes); endpoint.close(true); } }
@Test public void testEncodeDecode() throws Exception { props.putByteProperty(RandomUtil.randomSimpleString(), RandomUtil.randomByte()); props.putBytesProperty(RandomUtil.randomSimpleString(), RandomUtil.randomBytes()); props.putBytesProperty(RandomUtil.randomSimpleString(), null); props.putBooleanProperty(RandomUtil.randomSimpleString(), RandomUtil.randomBoolean()); props.putShortProperty(RandomUtil.randomSimpleString(), RandomUtil.randomShort()); props.putIntProperty(RandomUtil.randomSimpleString(), RandomUtil.randomInt()); props.putLongProperty(RandomUtil.randomSimpleString(), RandomUtil.randomLong()); props.putFloatProperty(RandomUtil.randomSimpleString(), RandomUtil.randomFloat()); props.putDoubleProperty(RandomUtil.randomSimpleString(), RandomUtil.randomDouble()); props.putCharProperty(RandomUtil.randomSimpleString(), RandomUtil.randomChar()); props.putSimpleStringProperty(RandomUtil.randomSimpleString(), RandomUtil.randomSimpleString()); props.putSimpleStringProperty(RandomUtil.randomSimpleString(), null); SimpleString keyToRemove = RandomUtil.randomSimpleString(); props.putSimpleStringProperty(keyToRemove, RandomUtil.randomSimpleString()); ActiveMQBuffer buffer = ActiveMQBuffers.dynamicBuffer(1024); props.encode(buffer.byteBuf()); Assert.assertEquals(props.getEncodeSize(), buffer.writerIndex()); TypedProperties decodedProps = new TypedProperties(); decodedProps.decode(buffer.byteBuf()); TypedPropertiesTest.assertEqualsTypeProperties(props, decodedProps); buffer.clear(); // After removing a property, you should still be able to encode the Property props.removeProperty(keyToRemove); props.encode(buffer.byteBuf()); Assert.assertEquals(props.getEncodeSize(), buffer.writerIndex()); }
private void testValidateUTFWithChars(final int size, final char c) { final char[] chars = new char[size]; Arrays.fill(chars, c); final String expectedUtf8String = new String(chars); final ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(4 * chars.length); UTF8Util.saveUTF(buffer.byteBuf(), expectedUtf8String); final byte[] expectedBytes = expectedUtf8String.getBytes(StandardCharsets.UTF_8); final int encodedSize = buffer.readUnsignedShort(); final byte[] realEncodedBytes = new byte[encodedSize]; buffer.getBytes(buffer.readerIndex(), realEncodedBytes); Assert.assertArrayEquals(expectedBytes, realEncodedBytes); }
ActiveMQBuffer buffer = ActiveMQBuffers.wrappedBuffer(data); String originatingNodeID = buffer.readString(); String uniqueID = buffer.readString(); int size = buffer.readInt();
@Test public void testEncodeDecodeEmpty() throws Exception { TypedProperties emptyProps = new TypedProperties(); ActiveMQBuffer buffer = ActiveMQBuffers.dynamicBuffer(1024); emptyProps.encode(buffer.byteBuf()); Assert.assertEquals(props.getEncodeSize(), buffer.writerIndex()); TypedProperties decodedProps = new TypedProperties(); decodedProps.decode(buffer.byteBuf()); TypedPropertiesTest.assertEqualsTypeProperties(emptyProps, decodedProps); }
/** * If you ever tried to debug XIDs you will know what this is about. * This will serialize and deserialize the XID to the same way it's going to be printed on server logs * or print-data. * <p> * This will convert to the same XID deserialized on the Server, hence we will be able to debug eventual stuff * * @param xid * @return */ public static Object convert(Xid xid) { ActiveMQBuffer buffer = ActiveMQBuffers.dynamicBuffer(200); XidCodecSupport.encodeXid(xid, buffer); Object obj = XidCodecSupport.decodeXid(buffer); return "xid=" + obj + ",clientXID=" + xid; }
@Override public synchronized void removeFileFactory(SequentialFileFactory fileFactory) throws Exception { ((JDBCSequentialFileFactory)fileFactory).destroy(); String tableName = factoryToTableName.remove(fileFactory); if (tableName != null) { SimpleString removeTableName = SimpleString.toSimpleString(tableName); JDBCSequentialFile directoryList = (JDBCSequentialFile) pagingFactoryFileFactory.createSequentialFile(DIRECTORY_NAME); directoryList.open(); int size = ((Long) directoryList.size()).intValue(); ActiveMQBuffer buffer = readActiveMQBuffer(directoryList, size); ActiveMQBuffer writeBuffer = ActiveMQBuffers.fixedBuffer(size); while (buffer.readableBytes() > 0) { SimpleString table = buffer.readSimpleString(); if (!removeTableName.equals(table)) { writeBuffer.writeSimpleString(table); } } directoryList.write(writeBuffer, true, null, false); directoryList.close(); } }
ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(0xffff + 4); UTF8Util.saveUTF(buffer.byteBuf(), str); Assert.fail("String is too big, supposed to throw an exception"); } catch (Exception ignored) { Assert.assertEquals("A buffer was supposed to be untouched since the string was too big", 0, buffer.writerIndex()); UTF8Util.saveUTF(buffer.byteBuf(), str); Assert.fail("Encoded String is too big, supposed to throw an exception"); } catch (Exception ignored) {
private void testValidateUTFOnDataInputStream(final String str, final ActiveMQBuffer wrap) throws Exception { UTF8Util.saveUTF(wrap.byteBuf(), str); DataInputStream data = new DataInputStream(new ByteArrayInputStream(wrap.toByteBuffer().array())); String newStr = data.readUTF(); Assert.assertEquals(str, newStr); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream outData = new DataOutputStream(byteOut); outData.writeUTF(str); ActiveMQBuffer buffer = ActiveMQBuffers.wrappedBuffer(byteOut.toByteArray()); newStr = UTF8Util.readUTF(buffer); Assert.assertEquals(str, newStr); }