org.apache.activemq.openwire.buffer.Buffer

Here are the examples of the java api class org.apache.activemq.openwire.buffer.Buffer taken from open source projects.

1. OpenWireStreamMessageTest#testWriteListToStreamCompressed()

Project: activemq-openwire
File: OpenWireStreamMessageTest.java
@Test
public void testWriteListToStreamCompressed() throws Exception {
    OpenWireStreamMessage message = new OpenWireStreamMessage();
    message.setUseCompression(true);
    // Add something that should compress well.
    elements.add("Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String" + "Repeating String");
    message.writeListToStream(Collections.emptyList());
    assertNull(message.getContent());
    message.writeListToStream(elements);
    assertNotNull(message.getContent());
    Buffer rawContent = message.getContent();
    Buffer processedContent = message.getPayload();
    assertTrue(rawContent.getLength() < processedContent.getLength());
}

2. WireFormatInfoMarshaledSizeTest#testMarshalThenUnmarshal1()

Project: activemq-openwire
File: WireFormatInfoMarshaledSizeTest.java
@Test
public void testMarshalThenUnmarshal1() throws Exception {
    final WireFormatInfo info = wireFormat.getPreferedWireFormatInfo();
    LOG.info("Original: {}", info);
    Buffer marshaledForm = wireFormat.marshal(info);
    assertNotNull(marshaledForm);
    Object result = wireFormat.unmarshal(marshaledForm);
    assertNotNull(result);
    assertTrue(result instanceof WireFormatInfo);
    WireFormatInfo duplicate = (WireFormatInfo) result;
    LOG.info("Duplicated: {}", duplicate);
    assertEquals(info.getVersion(), duplicate.getVersion());
    assertEquals(info.getCacheSize(), duplicate.getCacheSize());
    assertEquals(info.getMaxFrameSize(), duplicate.getMaxFrameSize());
    assertEquals(info.getMaxInactivityDuration(), duplicate.getMaxInactivityDuration());
    assertEquals(info.getMaxInactivityDurationInitalDelay(), duplicate.getMaxInactivityDurationInitalDelay());
    assertEquals(info.isCacheEnabled(), duplicate.isCacheEnabled());
    assertEquals(info.isSizePrefixDisabled(), duplicate.isSizePrefixDisabled());
    assertEquals(info.isTcpNoDelayEnabled(), duplicate.isTcpNoDelayEnabled());
    assertEquals(info.isStackTraceEnabled(), duplicate.isTcpNoDelayEnabled());
    assertEquals(info.isTightEncodingEnabled(), duplicate.isTightEncodingEnabled());
}

3. WireFormatInfoMarshaledSizeTest#testMarshalledSize1()

Project: activemq-openwire
File: WireFormatInfoMarshaledSizeTest.java
@Test
public void testMarshalledSize1() throws Exception {
    WireFormatInfo info = wireFormat.getPreferedWireFormatInfo();
    Buffer result = wireFormat.marshal(info);
    assertNotNull(result);
    LOG.info("Size of marshalled object: {}", result.getLength());
    assertEquals(getExpectedMarshaledSize(), result.getLength());
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(result.toByteArray());
    DataInputStream input = new DataInputStream(bytesIn);
    int size = input.readInt();
    assertEquals(getExpectedMarshaledSize() - 4, size);
}

4. OpenWireTextMessageTest#testGetBytes()

Project: activemq-openwire
File: OpenWireTextMessageTest.java
@Test
public void testGetBytes() throws Exception, IOException {
    OpenWireTextMessage msg = new OpenWireTextMessage();
    String str = "testText";
    msg.setText(str);
    msg.beforeMarshall(null);
    Buffer bytes = msg.getContent();
    msg = new OpenWireTextMessage();
    msg.setContent(bytes);
    assertEquals(msg.getText(), str);
}

5. OpenWireStreamMessage#readStreamToList()

Project: activemq-openwire
File: OpenWireStreamMessage.java
/**
     * Reads the contents of the StreamMessage instances into a single List<Object> instance
     * and returns it.  The read starts from the current position of the message which implies
     * that the list might not be a complete view of the message if any prior read operations
     * were invoked.
     *
     * @return a List containing the objects store in this message starting from the current position.
     *
     * @throws JMSException if an error occurs while reading the message.
     */
public List<Object> readStreamToList() throws IOException {
    if (!hasContent()) {
        return Collections.emptyList();
    }
    Buffer payload = getPayload();
    DataByteArrayInputStream dataIn = new DataByteArrayInputStream(payload);
    List<Object> result = new ArrayList<Object>();
    while (true) {
        try {
            result.add(readNextElement(dataIn));
        } catch (EOFException ex) {
            break;
        } catch (Exception e) {
            throw IOExceptionSupport.create(e);
        }
    }
    return result;
}

6. OpenWireObjectMessage#storeContent()

Project: activemq-openwire
File: OpenWireObjectMessage.java
@Override
public void storeContent() {
    Buffer bodyAsBytes = getContent();
    if (bodyAsBytes == null && object != null) {
        try {
            DataByteArrayOutputStream bytesOut = new DataByteArrayOutputStream();
            OutputStream os = bytesOut;
            if (isUseCompression()) {
                compressed = true;
                os = new DeflaterOutputStream(os);
            }
            DataOutputStream dataOut = new DataOutputStream(os);
            ObjectOutputStream objOut = new ObjectOutputStream(dataOut);
            objOut.writeObject(object);
            objOut.flush();
            objOut.reset();
            objOut.close();
            setContent(bytesOut.toBuffer());
        } catch (IOException ioe) {
            throw new RuntimeException(ioe.getMessage(), ioe);
        }
    }
}

7. OpenWireMessage#getPayload()

Project: activemq-openwire
File: OpenWireMessage.java
/**
     * Provides a fast way to read the message contents.
     *
     * This method, unlike the base class getContent method will perform any needed
     * decompression on a message that was received with a compressed payload.
     *
     * @return a the message contents, uncompressed as needed.
     *
     * @throws JMSException if an error occurs while accessing the message payload.
     */
public Buffer getPayload() throws IOException {
    Buffer data = getContent();
    if (data == null) {
        data = new Buffer(new byte[] {}, 0, 0);
    } else if (isCompressed()) {
        try {
            return decompress();
        } catch (Exception e) {
            throw IOExceptionSupport.create(e);
        }
    }
    return data;
}

8. OpenWireBytesMessage#doDecompress()

Project: activemq-openwire
File: OpenWireBytesMessage.java
@Override
protected Buffer doDecompress() throws IOException {
    Buffer compressed = getContent();
    Inflater inflater = new Inflater();
    DataByteArrayOutputStream decompressed = new DataByteArrayOutputStream();
    try {
        // Copy to avoid race on concurrent reads of compressed message payload.
        compressed = new Buffer(compressed);
        DataByteArrayInputStream compressedIn = new DataByteArrayInputStream(compressed);
        int length = compressedIn.readInt();
        compressedIn.close();
        byte[] data = Arrays.copyOfRange(compressed.getData(), 4, compressed.getLength());
        inflater.setInput(data);
        byte[] buffer = new byte[length];
        int count = inflater.inflate(buffer);
        decompressed.write(buffer, 0, count);
        return decompressed.toBuffer();
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        inflater.end();
        decompressed.close();
    }
}

9. OpenWireBytesMessage#getBodyBytes()

Project: activemq-openwire
File: OpenWireBytesMessage.java
/**
     * Provides a fast way to read the message contents.
     *
     * This method, unlike the base class getContent method will perform any
     * needed decompression on a message that was received with a compressed
     * payload.  The complete message body will then be read and returned in
     * a byte array copy.  Changes to the returned byte array are not reflected
     * in the underlying message contents.
     *
     * @return a copy of the message contents, uncompressed as needed.
     *
     * @throws IOException if an error occurs while accessing the message payload.
     */
public byte[] getBodyBytes() throws IOException {
    Buffer data = getPayload();
    if (data == null) {
        data = new Buffer(new byte[] {}, 0, 0);
    }
    return data.toByteArray();
}

10. WireFormatInfoMarshaledSizeTest#testMarshalThenUnmarshal4()

Project: activemq-openwire
File: WireFormatInfoMarshaledSizeTest.java
@Test
public void testMarshalThenUnmarshal4() throws Exception {
    final WireFormatInfo info = wireFormat.getPreferedWireFormatInfo();
    LOG.info("Original: {}", info);
    Buffer marshaledForm = wireFormat.marshal(info);
    assertNotNull(marshaledForm);
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(marshaledForm.data);
    DataInputStream dataIn = new DataInputStream(bytesIn);
    Object result = wireFormat.unmarshal(dataIn);
    assertNotNull(result);
    assertTrue(result instanceof WireFormatInfo);
    WireFormatInfo duplicate = (WireFormatInfo) result;
    LOG.info("Duplicated: {}", duplicate);
    assertEquals(info.getVersion(), duplicate.getVersion());
    assertEquals(info.getCacheSize(), duplicate.getCacheSize());
    assertEquals(info.getMaxFrameSize(), duplicate.getMaxFrameSize());
    assertEquals(info.getMaxInactivityDuration(), duplicate.getMaxInactivityDuration());
    assertEquals(info.getMaxInactivityDurationInitalDelay(), duplicate.getMaxInactivityDurationInitalDelay());
    assertEquals(info.isCacheEnabled(), duplicate.isCacheEnabled());
    assertEquals(info.isSizePrefixDisabled(), duplicate.isSizePrefixDisabled());
    assertEquals(info.isTcpNoDelayEnabled(), duplicate.isTcpNoDelayEnabled());
    assertEquals(info.isStackTraceEnabled(), duplicate.isTcpNoDelayEnabled());
    assertEquals(info.isTightEncodingEnabled(), duplicate.isTightEncodingEnabled());
}

11. WireFormatInfoMarshaledSizeTest#testMarshalThenUnmarshal3()

Project: activemq-openwire
File: WireFormatInfoMarshaledSizeTest.java
@Test
public void testMarshalThenUnmarshal3() throws Exception {
    final WireFormatInfo info = wireFormat.getPreferedWireFormatInfo();
    LOG.info("Original: {}", info);
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(bytesOut);
    wireFormat.marshal(info, dataOut);
    dataOut.close();
    Buffer marshaledForm = new Buffer(bytesOut.toByteArray());
    Object result = wireFormat.unmarshal(marshaledForm);
    assertNotNull(result);
    assertTrue(result instanceof WireFormatInfo);
    WireFormatInfo duplicate = (WireFormatInfo) result;
    LOG.info("Duplicated: {}", duplicate);
    assertEquals(info.getVersion(), duplicate.getVersion());
    assertEquals(info.getCacheSize(), duplicate.getCacheSize());
    assertEquals(info.getMaxFrameSize(), duplicate.getMaxFrameSize());
    assertEquals(info.getMaxInactivityDuration(), duplicate.getMaxInactivityDuration());
    assertEquals(info.getMaxInactivityDurationInitalDelay(), duplicate.getMaxInactivityDurationInitalDelay());
    assertEquals(info.isCacheEnabled(), duplicate.isCacheEnabled());
    assertEquals(info.isSizePrefixDisabled(), duplicate.isSizePrefixDisabled());
    assertEquals(info.isTcpNoDelayEnabled(), duplicate.isTcpNoDelayEnabled());
    assertEquals(info.isStackTraceEnabled(), duplicate.isTcpNoDelayEnabled());
    assertEquals(info.isTightEncodingEnabled(), duplicate.isTightEncodingEnabled());
}

12. OpenWireMessageTest#fakeUnmarshal()

Project: activemq-openwire
File: OpenWireMessageTest.java
private void fakeUnmarshal(OpenWireObjectMessage message) throws Exception {
    OpenWireFormat format = new OpenWireFormat(OpenWireFormat.DEFAULT_WIRE_VERSION);
    message.beforeMarshall(format);
    message.afterMarshall(format);
    Buffer seq = message.getMarshalledProperties();
    message.clearProperties();
    message.setMarshalledProperties(seq);
}

13. DataStructureTestSupport#marshalAndUnmarshall()

Project: activemq-openwire
File: DataStructureTestSupport.java
protected Object marshalAndUnmarshall(Object original, OpenWireFormat wireFormat) throws IOException {
    Buffer packet = wireFormat.marshal(original);
    return wireFormat.unmarshal(packet);
}

14. OpenWireMessage#hasContent()

Project: activemq-openwire
File: OpenWireMessage.java
/**
     * @returns true if the message has data in its body, false if empty.
     */
public boolean hasContent() {
    Buffer content = getContent();
    if (content == null || content.isEmpty()) {
        return false;
    }
    return true;
}

15. OpenWireBytesMessage#doCompress()

Project: activemq-openwire
File: OpenWireBytesMessage.java
@Override
protected void doCompress() throws IOException {
    compressed = true;
    Buffer bytes = getContent();
    if (bytes != null) {
        int length = bytes.getLength();
        DataByteArrayOutputStream compressed = new DataByteArrayOutputStream();
        compressed.write(new byte[4]);
        Deflater deflater = new Deflater();
        try {
            deflater.setInput(bytes.data);
            deflater.finish();
            byte[] buffer = new byte[1024];
            while (!deflater.finished()) {
                int count = deflater.deflate(buffer);
                compressed.write(buffer, 0, count);
            }
            compressed.writeInt(0, length);
            setContent(compressed.toBuffer());
        } finally {
            deflater.end();
            compressed.close();
        }
    }
}

16. Message#doCompress()

Project: activemq-openwire
File: Message.java
protected void doCompress() throws IOException {
    compressed = true;
    Buffer bytes = getContent();
    DataByteArrayOutputStream bytesOut = new DataByteArrayOutputStream();
    OutputStream os = new DeflaterOutputStream(bytesOut);
    os.write(bytes.data, bytes.offset, bytes.length);
    os.close();
    setContent(bytesOut.toBuffer());
}

17. OpenWireFormat#marshal()

Project: activemq-openwire
File: OpenWireFormat.java
public synchronized Buffer marshal(Object command) throws IOException {
    if (cacheEnabled) {
        runMarshallCacheEvictionSweep();
    }
    Buffer sequence = null;
    int size = 1;
    if (command != null) {
        DataStructure c = (DataStructure) command;
        byte type = c.getDataStructureType();
        DataStreamMarshaller dsm = dataMarshallers[type & 0xFF];
        if (dsm == null) {
            throw new IOException("Unknown data type: " + type);
        }
        if (tightEncodingEnabled) {
            BooleanStream bs = new BooleanStream();
            size += dsm.tightMarshal1(this, c, bs);
            size += bs.marshalledSize();
            bytesOut.restart(size);
            if (!sizePrefixDisabled) {
                bytesOut.writeInt(size);
            }
            bytesOut.writeByte(type);
            bs.marshal(bytesOut);
            dsm.tightMarshal2(this, c, bytesOut, bs);
            sequence = bytesOut.toBuffer();
        } else {
            bytesOut.restart();
            if (!sizePrefixDisabled) {
                // we don't know the final size yet but write this here for now.
                bytesOut.writeInt(0);
            }
            bytesOut.writeByte(type);
            dsm.looseMarshal(this, c, bytesOut);
            if (!sizePrefixDisabled) {
                size = bytesOut.size() - 4;
                bytesOut.writeInt(0, size);
            }
            sequence = bytesOut.toBuffer();
        }
    } else {
        bytesOut.restart(5);
        bytesOut.writeInt(size);
        bytesOut.writeByte(NULL_TYPE);
        sequence = bytesOut.toBuffer();
    }
    return sequence;
}

18. BaseDataStreamMarshaller#looseUnmarshalByteSequence()

Project: activemq-openwire
File: BaseDataStreamMarshaller.java
protected Buffer looseUnmarshalByteSequence(DataInput dataIn) throws IOException {
    Buffer rc = null;
    if (dataIn.readBoolean()) {
        int size = dataIn.readInt();
        byte[] t = new byte[size];
        dataIn.readFully(t);
        rc = new Buffer(t, 0, size);
    }
    return rc;
}

19. BaseDataStreamMarshaller#tightUnmarshalByteSequence()

Project: activemq-openwire
File: BaseDataStreamMarshaller.java
protected Buffer tightUnmarshalByteSequence(DataInput dataIn, BooleanStream bs) throws IOException {
    Buffer rc = null;
    if (bs.readBoolean()) {
        int size = dataIn.readInt();
        byte[] t = new byte[size];
        dataIn.readFully(t);
        return new Buffer(t, 0, size);
    }
    return rc;
}