@Override public void encodeRest(final ActiveMQBuffer buffer) { buffer.writeByte(journalID); buffer.writeBoolean(operation.toBoolean()); buffer.writeLong(id); buffer.writeByte(journalRecordType); buffer.writeInt(encodingData.getEncodeSize()); encodingData.encode(buffer); }
private void manualEncodeVertxMessageBody(ActiveMQBuffer bodyBuffer, Object body, int type) { switch (type) { case VertxConstants.TYPE_BOOLEAN: bodyBuffer.writeBoolean(((Boolean) body)); break; case VertxConstants.TYPE_BUFFER: Buffer buff = (Buffer) body; int len = buff.length(); bodyBuffer.writeInt(len); bodyBuffer.writeBytes(((Buffer) body).getBytes()); break; case VertxConstants.TYPE_BYTEARRAY: byte[] bytes = (byte[]) body; bodyBuffer.writeInt(bytes.length); bodyBuffer.writeBytes(bytes); break; case VertxConstants.TYPE_BYTE: bodyBuffer.writeByte((byte) body); break; case VertxConstants.TYPE_CHARACTER: bodyBuffer.writeChar((Character) body); break; case VertxConstants.TYPE_DOUBLE: bodyBuffer.writeDouble((double) body); break; case VertxConstants.TYPE_FLOAT: bodyBuffer.writeFloat((Float) body); break; case VertxConstants.TYPE_INT: bodyBuffer.writeInt((Integer) body); break; case VertxConstants.TYPE_LONG: bodyBuffer.writeLong((Long) body); break; case VertxConstants.TYPE_SHORT: bodyBuffer.writeShort((Short) body); break; case VertxConstants.TYPE_STRING: case VertxConstants.TYPE_PING: bodyBuffer.writeString((String) body); break; case VertxConstants.TYPE_JSON_OBJECT: bodyBuffer.writeString(((JsonObject) body).encode()); break; case VertxConstants.TYPE_JSON_ARRAY: bodyBuffer.writeString(((JsonArray) body).encode()); break; case VertxConstants.TYPE_REPLY_FAILURE: ReplyException except = (ReplyException) body; bodyBuffer.writeInt(except.failureType().toInt()); bodyBuffer.writeInt(except.failureCode()); bodyBuffer.writeString(except.getMessage()); break; default: throw new IllegalArgumentException("Invalid body type: " + type); } }
@Test public void testCreateBuffer() throws Exception { EmbeddedChannel channel = createChannel(); NettyConnection conn = new NettyConnection(emptyMap, channel, new MyListener(), false, false); final int size = 1234; ActiveMQBuffer buff = conn.createTransportBuffer(size); buff.writeByte((byte) 0x00); // Netty buffer does lazy initialization. Assert.assertEquals(size, buff.capacity()); }
public ActiveMQBuffer encode(final RemotingConnection connection) { ActiveMQBuffer buffer = connection.createTransportBuffer(PacketImpl.INITIAL_PACKET_SIZE); // The standard header fields buffer.writeInt(0); // The length gets filled in at the end buffer.writeByte(type); buffer.writeLong(channelID); encodeRest(buffer); size = buffer.writerIndex(); // The length doesn't include the actual length byte int len = size - DataConstants.SIZE_INT; buffer.setInt(0, len); return buffer; }
@Override public ServerMessage inbound(Object message) throws Exception { Message messageSend = (Message) message; ServerMessageImpl coreMessage = new ServerMessageImpl(-1, messageSend.getSize()); String type = messageSend.getType(); if (type != null) { coreMessage.putStringProperty(new SimpleString("JMSType"), new SimpleString(type)); } coreMessage.setDurable(messageSend.isPersistent()); coreMessage.setExpiration(messageSend.getExpiration()); coreMessage.setPriority(messageSend.getPriority()); coreMessage.setTimestamp(messageSend.getTimestamp()); byte coreType = toCoreType(messageSend.getDataStructureType()); coreMessage.setType(coreType); ActiveMQBuffer body = coreMessage.getBodyBuffer(); ByteSequence contents = messageSend.getContent(); if (contents == null && coreType == org.apache.activemq.artemis.api.core.Message.TEXT_TYPE) { body.writeNullableString(null); } else if (contents != null) { boolean messageCompressed = messageSend.isCompressed(); if (messageCompressed) { coreMessage.putBooleanProperty(AMQ_MSG_COMPRESSED, messageCompressed); } switch (coreType) { case org.apache.activemq.artemis.api.core.Message.TEXT_TYPE: InputStream tis = new ByteArrayInputStream(contents); if (messageCompressed) { tis = new InflaterInputStream(tis); } DataInputStream tdataIn = new DataInputStream(tis); String text = MarshallingSupport.readUTF8(tdataIn); tdataIn.close(); body.writeNullableSimpleString(new SimpleString(text)); break; case org.apache.activemq.artemis.api.core.Message.MAP_TYPE: InputStream mis = new ByteArrayInputStream(contents); if (messageCompressed) { mis = new InflaterInputStream(mis); } DataInputStream mdataIn = new DataInputStream(mis); Map<String, Object> map = MarshallingSupport.unmarshalPrimitiveMap(mdataIn); mdataIn.close(); TypedProperties props = new TypedProperties(); loadMapIntoProperties(props, map); props.encode(body); break; case org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE: if (messageCompressed) { try (InputStream ois = new InflaterInputStream(new ByteArrayInputStream(contents)); org.apache.activemq.util.ByteArrayOutputStream decompressed = new org.apache.activemq.util.ByteArrayOutputStream()) { byte[] buf = new byte[1024]; int n = ois.read(buf); while (n != -1) { decompressed.write(buf, 0, n); n = ois.read(); } // read done contents = decompressed.toByteSequence(); } } body.writeInt(contents.length); body.writeBytes(contents.data, contents.offset, contents.length); break; case org.apache.activemq.artemis.api.core.Message.STREAM_TYPE: InputStream sis = new ByteArrayInputStream(contents); if (messageCompressed) { sis = new InflaterInputStream(sis); } DataInputStream sdis = new DataInputStream(sis); int stype = sdis.read(); while (stype != -1) { switch (stype) { case MarshallingSupport.BOOLEAN_TYPE: body.writeByte(DataConstants.BOOLEAN); body.writeBoolean(sdis.readBoolean()); break; case MarshallingSupport.BYTE_TYPE: body.writeByte(DataConstants.BYTE); body.writeByte(sdis.readByte()); break; case MarshallingSupport.BYTE_ARRAY_TYPE: body.writeByte(DataConstants.BYTES); int slen = sdis.readInt(); byte[] sbytes = new byte[slen]; sdis.read(sbytes); body.writeInt(slen); body.writeBytes(sbytes); break; case MarshallingSupport.CHAR_TYPE: body.writeByte(DataConstants.CHAR); char schar = sdis.readChar(); body.writeShort((short) schar); break; case MarshallingSupport.DOUBLE_TYPE: body.writeByte(DataConstants.DOUBLE); double sdouble = sdis.readDouble(); body.writeLong(Double.doubleToLongBits(sdouble)); break; case MarshallingSupport.FLOAT_TYPE: body.writeByte(DataConstants.FLOAT); float sfloat = sdis.readFloat(); body.writeInt(Float.floatToIntBits(sfloat)); break; case MarshallingSupport.INTEGER_TYPE: body.writeByte(DataConstants.INT); body.writeInt(sdis.readInt()); break; case MarshallingSupport.LONG_TYPE: body.writeByte(DataConstants.LONG); body.writeLong(sdis.readLong()); break; case MarshallingSupport.SHORT_TYPE: body.writeByte(DataConstants.SHORT); body.writeShort(sdis.readShort()); break; case MarshallingSupport.STRING_TYPE: body.writeByte(DataConstants.STRING); String sstring = sdis.readUTF(); body.writeNullableString(sstring); break; case MarshallingSupport.BIG_STRING_TYPE: body.writeByte(DataConstants.STRING); String sbigString = MarshallingSupport.readUTF8(sdis); body.writeNullableString(sbigString); break; case MarshallingSupport.NULL: body.writeByte(DataConstants.STRING); body.writeNullableString(null); break; default: // something we don't know, ignore break; } stype = sdis.read(); } sdis.close(); break; case org.apache.activemq.artemis.api.core.Message.BYTES_TYPE: if (messageCompressed) { Inflater inflater = new Inflater(); try (org.apache.activemq.util.ByteArrayOutputStream decompressed = new org.apache.activemq.util.ByteArrayOutputStream()) { int length = ByteSequenceData.readIntBig(contents); contents.offset = 0; byte[] data = Arrays.copyOfRange(contents.getData(), 4, contents.getLength()); inflater.setInput(data); byte[] buffer = new byte[length]; int count = inflater.inflate(buffer); decompressed.write(buffer, 0, count); contents = decompressed.toByteSequence(); } catch (Exception e) { throw new IOException(e); } finally { inflater.end(); } } body.writeBytes(contents.data, contents.offset, contents.length); break; default: if (messageCompressed) { try (org.apache.activemq.util.ByteArrayOutputStream decompressed = new org.apache.activemq.util.ByteArrayOutputStream(); OutputStream os = new InflaterOutputStream(decompressed)) { os.write(contents.data, contents.offset, contents.getLength()); contents = decompressed.toByteSequence(); } catch (Exception e) { throw new IOException(e); } } body.writeBytes(contents.data, contents.offset, contents.length); break; } } // amq specific coreMessage.putLongProperty(AMQ_MSG_ARRIVAL, messageSend.getArrival()); coreMessage.putLongProperty(AMQ_MSG_BROKER_IN_TIME, messageSend.getBrokerInTime()); BrokerId[] brokers = messageSend.getBrokerPath(); if (brokers != null) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < brokers.length; i++) { builder.append(brokers[i].getValue()); if (i != (brokers.length - 1)) { builder.append(","); // is this separator safe? } } coreMessage.putStringProperty(AMQ_MSG_BROKER_PATH, builder.toString()); } BrokerId[] cluster = messageSend.getCluster(); if (cluster != null) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < cluster.length; i++) { builder.append(cluster[i].getValue()); if (i != (cluster.length - 1)) { builder.append(","); // is this separator safe? } } coreMessage.putStringProperty(AMQ_MSG_CLUSTER, builder.toString()); } coreMessage.putIntProperty(AMQ_MSG_COMMAND_ID, messageSend.getCommandId()); String corrId = messageSend.getCorrelationId(); if (corrId != null) { coreMessage.putStringProperty("JMSCorrelationID", corrId); } DataStructure ds = messageSend.getDataStructure(); if (ds != null) { ByteSequence dsBytes = marshaller.marshal(ds); dsBytes.compact(); coreMessage.putBytesProperty(AMQ_MSG_DATASTRUCTURE, dsBytes.data); } String groupId = messageSend.getGroupID(); if (groupId != null) { coreMessage.putStringProperty(AMQ_MSG_GROUP_ID, groupId); } coreMessage.putIntProperty(AMQ_MSG_GROUP_SEQUENCE, messageSend.getGroupSequence()); MessageId messageId = messageSend.getMessageId(); ByteSequence midBytes = marshaller.marshal(messageId); midBytes.compact(); coreMessage.putBytesProperty(AMQ_MSG_MESSAGE_ID, midBytes.data); ProducerId producerId = messageSend.getProducerId(); if (producerId != null) { ByteSequence producerIdBytes = marshaller.marshal(producerId); producerIdBytes.compact(); coreMessage.putBytesProperty(AMQ_MSG_PRODUCER_ID, producerIdBytes.data); } ByteSequence propBytes = messageSend.getMarshalledProperties(); if (propBytes != null) { propBytes.compact(); coreMessage.putBytesProperty(AMQ_MSG_MARSHALL_PROP, propBytes.data); // unmarshall properties to core so selector will work Map<String, Object> props = messageSend.getProperties(); // Map<String, Object> props = MarshallingSupport.unmarshalPrimitiveMap(new // DataInputStream(new ByteArrayInputStream(propBytes))); for (Entry<String, Object> ent : props.entrySet()) { Object value = ent.getValue(); try { coreMessage.putObjectProperty(ent.getKey(), value); } catch (ActiveMQPropertyConversionException e) { coreMessage.putStringProperty(ent.getKey(), value.toString()); } } } ActiveMQDestination replyTo = messageSend.getReplyTo(); if (replyTo != null) { ByteSequence replyToBytes = marshaller.marshal(replyTo); replyToBytes.compact(); coreMessage.putBytesProperty(AMQ_MSG_REPLY_TO, replyToBytes.data); } ConsumerId consumerId = messageSend.getTargetConsumerId(); String userId = messageSend.getUserID(); if (userId != null) { coreMessage.putStringProperty(AMQ_MSG_USER_ID, userId); } coreMessage.putBooleanProperty(AMQ_MSG_DROPPABLE, messageSend.isDroppable()); ActiveMQDestination origDest = messageSend.getOriginalDestination(); if (origDest != null) { ByteSequence origDestBytes = marshaller.marshal(origDest); origDestBytes.compact(); coreMessage.putBytesProperty(AMQ_MSG_ORIG_DESTINATION, origDestBytes.data); } return coreMessage; }
@Override public void write(int b) throws IOException { bufferOut.writeByte((byte) (b & 0xff)); }