/** * Extract a byte array from the given {@link BytesMessage}. * @param message the message to convert * @return the resulting byte array * @throws JMSException if thrown by JMS methods */ protected byte[] extractByteArrayFromMessage(BytesMessage message) throws JMSException { byte[] bytes = new byte[(int) message.getBodyLength()]; message.readBytes(bytes); return bytes; }
/** * Unmarshal the given {@link BytesMessage} into an object. * @param message the message * @param unmarshaller the unmarshaller to use * @return the unmarshalled object * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors * @throws XmlMappingException in case of OXM mapping errors * @see Unmarshaller#unmarshal(Source) */ protected Object unmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException { byte[] bytes = new byte[(int) message.getBodyLength()]; message.readBytes(bytes); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); StreamSource source = new StreamSource(bis); return unmarshaller.unmarshal(source); }
/** * Foreign message constructor */ public ActiveMQBytesMessage(final BytesMessage foreign, final ClientSession session) throws JMSException { super(foreign, ActiveMQBytesMessage.TYPE, session); foreign.reset(); byte[] buffer = new byte[1024]; int n = foreign.readBytes(buffer); while (n != -1) { writeBytes(buffer, 0, n); n = foreign.readBytes(buffer); } }
@Override public int read(byte[] buffer, int offset, int length) throws IOException { try { if (offset == 0) { return this.message.readBytes(buffer, length); } else { return super.read(buffer, offset, length); } } catch (JMSException e) { throw new IOException(e.toString()); } }
@Override public int read(byte[] buffer) throws IOException { try { return this.message.readBytes(buffer); } catch (JMSException e) { throw new IOException(e.toString()); } } }
/** * Convert a BytesMessage to a Java Object with the specified type. * @param message the input message * @param targetJavaType the target type * @return the message converted to an object * @throws JMSException if thrown by JMS * @throws IOException in case of I/O errors */ protected Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType) throws JMSException, IOException { String encoding = this.encoding; if (this.encodingPropertyName != null && message.propertyExists(this.encodingPropertyName)) { encoding = message.getStringProperty(this.encodingPropertyName); } byte[] bytes = new byte[(int) message.getBodyLength()]; message.readBytes(bytes); try { String body = new String(bytes, encoding); return this.objectMapper.readValue(body, targetJavaType); } catch (UnsupportedEncodingException ex) { throw new MessageConversionException("Cannot convert bytes to String", ex); } }
private static byte[] getMessageBytes(BytesMessage message) throws JMSException { final long byteCount = message.getBodyLength(); if (byteCount > Integer.MAX_VALUE) { throw new JMSException("Incoming message cannot be written to a FlowFile because its size is " + byteCount + " bytes, and the maximum size that this processor can handle is " + Integer.MAX_VALUE); } byte[] bytes = new byte[(int) byteCount]; message.readBytes(bytes); return bytes; }
public boolean evaluate(Message message) throws JMSException { if (message instanceof TextMessage) { String text = ((TextMessage)message).getText(); return evaluate(text); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage)message; byte data[] = new byte[(int)bm.getBodyLength()]; bm.readBytes(data); return evaluate(data); } return false; }
public boolean evaluate(Message message) throws JMSException { if (message instanceof TextMessage) { String text = ((TextMessage)message).getText(); return evaluate(text); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage)message; byte data[] = new byte[(int)bm.getBodyLength()]; bm.readBytes(data); return evaluate(data); } return false; }
long length = ((BytesMessage) msg).getBodyLength(); byte[] bytes = new byte[(int) length]; ((BytesMessage) msg).readBytes(bytes); LOG.info("BytesMessage as text string: " + new String(bytes));
int count = bytesMessage.readBytes(body); if (count != length) { throw new JMSException("Unable to read full message. " +
@Test public void fromBytesMessage() throws Exception { BytesMessage bytesMessageMock = mock(BytesMessage.class); Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar"); byte[] bytes = "{\"foo\":\"bar\"}".getBytes(); final ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); given(bytesMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName()); given(bytesMessageMock.propertyExists("__encoding__")).willReturn(false); given(bytesMessageMock.getBodyLength()).willReturn(new Long(bytes.length)); given(bytesMessageMock.readBytes(any(byte[].class))).willAnswer( new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { return byteStream.read((byte[]) invocation.getArguments()[0]); } }); Object result = converter.fromMessage(bytesMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void testWithMessageContentsDelegateForBytesMessage() throws Exception { BytesMessage bytesMessage = mock(BytesMessage.class); // BytesMessage contents must be unwrapped... given(bytesMessage.getBodyLength()).willReturn(new Long(TEXT.getBytes().length)); given(bytesMessage.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { byte[] bytes = (byte[]) invocation.getArguments()[0]; ByteArrayInputStream inputStream = new ByteArrayInputStream(TEXT.getBytes()); return inputStream.read(bytes); } }); MessageContentsDelegate delegate = mock(MessageContentsDelegate.class); MessageListenerAdapter adapter = new MessageListenerAdapter(delegate); adapter.onMessage(bytesMessage); verify(delegate).handleMessage(TEXT.getBytes()); }
@Test public void fromBytesMessage() throws Exception { BytesMessage bytesMessageMock = mock(BytesMessage.class); Object unmarshalled = new Object(); given(bytesMessageMock.getBodyLength()).willReturn(10L); given(bytesMessageMock.readBytes(isA(byte[].class))).willReturn(0); given(unmarshallerMock.unmarshal(isA(Source.class))).willReturn(unmarshalled); Object result = converter.fromMessage(bytesMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void testByteArrayConversion() throws JMSException { Session session = mock(Session.class); BytesMessage message = mock(BytesMessage.class); byte[] content = "test".getBytes(); final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content); given(session.createBytesMessage()).willReturn(message); given(message.getBodyLength()).willReturn((long) content.length); given(message.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { return byteArrayInputStream.read((byte[]) invocation.getArguments()[0]); } }); SimpleMessageConverter converter = new SimpleMessageConverter(); Message msg = converter.toMessage(content, session); assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length); verify(message).writeBytes(content); }
@Override public int read(byte[] b) throws IOException { try { return delegate.readBytes(b); } catch (JMSException e) { throw new IOException(e); } }
public boolean evaluate(Message message) throws JMSException { if (message instanceof TextMessage) { String text = ((TextMessage)message).getText(); return evaluate(text); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage)message; byte data[] = new byte[(int)bm.getBodyLength()]; bm.readBytes(data); return evaluate(data); } return false; }
public boolean evaluate(Message message) throws JMSException { if (message instanceof TextMessage) { String text = ((TextMessage)message).getText(); return evaluate(text); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage)message; byte data[] = new byte[(int)bm.getBodyLength()]; bm.readBytes(data); return evaluate(data); } return false; }
public boolean evaluate(Message message) throws JMSException { if (message instanceof TextMessage) { String text = ((TextMessage)message).getText(); return evaluate(text); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage)message; byte data[] = new byte[(int)bm.getBodyLength()]; bm.readBytes(data); return evaluate(data); } return false; }
public boolean evaluate(Message message) throws JMSException { if (message instanceof TextMessage) { String text = ((TextMessage)message).getText(); return evaluate(text); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage)message; byte data[] = new byte[(int)bm.getBodyLength()]; bm.readBytes(data); return evaluate(data); } return false; }