下面列出了怎么用javax.jms.MessageEOFException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public byte readByte() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).byteValue();
} else if (value instanceof String) {
byte result = Byte.parseByte((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public char readChar() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Character) {
position++;
return ((Character) value).charValue();
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public float readFloat() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Float) {
position++;
return ((Float) value).floatValue();
} else if (value instanceof String) {
float result = Float.parseFloat((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Test
public void testClearBodyOnNewMessageRemovesExistingValues() throws Exception {
JmsStreamMessage streamMessage = factory.createStreamMessage();
streamMessage.writeBoolean(true);
streamMessage.clearBody();
streamMessage.writeBoolean(false);
streamMessage.reset();
// check we get only the value added after the clear
assertFalse("expected value added after the clear", streamMessage.readBoolean());
try {
streamMessage.readBoolean();
fail("Expected exception to be thrown");
} catch (MessageEOFException meofe) {
// expected
}
}
@Test
public void testPopFullyReadListThrowsMEOFE() throws Exception {
Message message = Message.Factory.create();
List<Object> list = new ArrayList<Object>();
list.add(Boolean.FALSE);
message.setBody(new AmqpSequence(list));
AmqpJmsStreamMessageFacade amqpStreamMessageFacade = createReceivedStreamMessageFacade(createMockAmqpConsumer(), message);
assertEquals("Unexpected value retrieved", Boolean.FALSE, amqpStreamMessageFacade.peek());
amqpStreamMessageFacade.pop();
try {
amqpStreamMessageFacade.pop();
fail("expected exception to be thrown");
} catch (MessageEOFException meofe) {
// expected
}
}
@Override
public boolean readBoolean() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Boolean) {
position++;
return ((Boolean) value).booleanValue();
} else if (value instanceof String) {
boolean result = Boolean.valueOf((String) value).booleanValue();
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public short readShort() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).shortValue();
} else if (value instanceof Short) {
position++;
return ((Short) value).shortValue();
} else if (value instanceof String) {
short result = Short.parseShort((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public int readInt() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).intValue();
} else if (value instanceof Short) {
position++;
return ((Short) value).intValue();
} else if (value instanceof Integer) {
position++;
return ((Integer) value).intValue();
} else if (value instanceof String) {
int result = Integer.parseInt((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public long readLong() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).longValue();
} else if (value instanceof Short) {
position++;
return ((Short) value).longValue();
} else if (value instanceof Integer) {
position++;
return ((Integer) value).longValue();
} else if (value instanceof Long) {
position++;
return ((Long) value).longValue();
} else if (value instanceof String) {
long result = Long.parseLong((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public double readDouble() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Float) {
position++;
return ((Float) value).doubleValue();
} else if (value instanceof Double) {
position++;
return ((Double) value).doubleValue();
} else if (value instanceof String) {
double result = Double.parseDouble((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public Object readObject() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
position++;
offset = 0;
return value;
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
private void doReadTypeFromEmptyMessage(final TypeReader reader) throws Exception {
ActiveMQStreamMessage message = new ActiveMQStreamMessage();
message.reset();
try {
reader.readType(message);
Assert.fail("MessageEOFException");
} catch (MessageEOFException e) {
}
}
public ActiveMQStreamMessage(final StreamMessage foreign, final ClientSession session) throws JMSException {
super(foreign, ActiveMQStreamMessage.TYPE, session);
foreign.reset();
try {
while (true) {
Object obj = foreign.readObject();
writeObject(obj);
}
} catch (MessageEOFException e) {
// Ignore
}
}
@Override
public boolean readBoolean() throws JMSException {
checkRead();
try {
return bytesReadBoolean(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public byte readByte() throws JMSException {
checkRead();
try {
return bytesReadByte(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public int readUnsignedByte() throws JMSException {
checkRead();
try {
return bytesReadUnsignedByte(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public short readShort() throws JMSException {
checkRead();
try {
return bytesReadShort(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public int readUnsignedShort() throws JMSException {
checkRead();
try {
return bytesReadUnsignedShort(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public char readChar() throws JMSException {
checkRead();
try {
return bytesReadChar(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public int readInt() throws JMSException {
checkRead();
try {
return bytesReadInt(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public long readLong() throws JMSException {
checkRead();
try {
return bytesReadLong(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public float readFloat() throws JMSException {
checkRead();
try {
return bytesReadFloat(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public double readDouble() throws JMSException {
checkRead();
try {
return bytesReadDouble(message.getBodyBuffer());
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public Object peek() throws MessageEOFException {
if (list.isEmpty() || position >= list.size()) {
throw new MessageEOFException("Attempt to read past end of stream");
}
Object object = list.get(position);
if (object instanceof Binary) {
// Copy to a byte[], ensure we copy only the required portion.
Binary bin = ((Binary) object);
object = Arrays.copyOfRange(bin.getArray(), bin.getArrayOffset(), bin.getLength());
}
return object;
}
@Override
public void pop() throws MessageEOFException {
if (list.isEmpty() || position >= list.size()) {
throw new MessageEOFException("Attempt to read past end of stream");
}
position++;
}
@Test
public void testEmptyForeignStreamMessageTransformCreateNewMessage() throws JMSException {
ForeignJmsStreamMessage foreignMessage = new ForeignJmsStreamMessage();
JmsMessage transformed = JmsMessageTransformation.transformMessage(createMockJmsConnection(), foreignMessage);
assertNotSame(foreignMessage, transformed);
assertFalse(transformed.equals(foreignMessage));
assertTrue(transformed instanceof JmsStreamMessage);
JmsStreamMessage message = (JmsStreamMessage) transformed;
message.reset();
try {
message.readBoolean();
} catch (MessageEOFException ex) {}
}
@Test
public void tesAbnormalForeignStreamMessageTransformCreateNewMessage() throws JMSException {
ForeignJmsStreamMessage foreignMessage = new ForeignJmsStreamMessage();
foreignMessage.writeObject(true);
foreignMessage.reset();
foreignMessage = Mockito.spy(foreignMessage);
// Test for an odd StreamMessage that return null instead of throwing a MessageEOFException
Mockito.when(foreignMessage.readObject()).thenReturn(true).
thenReturn(false).
thenReturn(true).
thenReturn(null);
JmsMessage transformed = JmsMessageTransformation.transformMessage(createMockJmsConnection(), foreignMessage);
assertNotSame(foreignMessage, transformed);
assertFalse(transformed.equals(foreignMessage));
assertTrue(transformed instanceof JmsStreamMessage);
JmsStreamMessage message = (JmsStreamMessage) transformed;
message.reset();
assertTrue(message.readBoolean());
assertFalse(message.readBoolean());
assertTrue(message.readBoolean());
try {
message.readBoolean();
} catch (MessageEOFException ex) {}
}
@Test
public void testReadWithEmptyStreamThrowsMEOFE() throws Exception {
JmsStreamMessage streamMessage = factory.createStreamMessage();
streamMessage.reset();
try {
streamMessage.readBoolean();
fail("Expected exception to be thrown as message has no content");
} catch (MessageEOFException meofe) {
// expected
}
}
@Override
public Object peek() throws MessageEOFException {
if (stream.isEmpty() || index + 1 >= stream.size()) {
throw new MessageEOFException("Attempted to read past the end of the stream");
}
return stream.get(index + 1);
}
@Override
public void pop() throws MessageEOFException {
if (stream.isEmpty() || index + 1 >= stream.size()) {
throw new MessageEOFException("Attempted to read past the end of the stream");
}
index++;
}