@Override public synchronized void handleLargeMessageContinuation(final byte[] chunk, final int flowControlSize, final boolean isContinues) throws Exception { if (closing) { return; } if (currentLargeMessageController == null) { if (logger.isTraceEnabled()) { logger.trace(this + "::Sending back credits for largeController = null " + flowControlSize); } flowControl(flowControlSize, false); } else { currentLargeMessageController.addPacket(chunk, flowControlSize, isContinues); } }
public int readBytes(final GatheringByteChannel out, final int length) throws IOException { int readBytes = getBytes((int) readerIndex, out, length); readerIndex += readBytes; return readBytes; }
@Override public String readNullableString() { int b = readByte(); if (b == DataConstants.NULL) { return null; } else { return readString(); } }
@Override public SimpleString readNullableSimpleString() { int b = readByte(); if (b == DataConstants.NULL) { return null; } else { return readSimpleString(); } }
@Override public String readString() { int len = readInt(); if (len < 9) { char[] chars = new char[len]; for (int i = 0; i < len; i++) { chars[i] = (char) readShort(); } return new String(chars); } else if (len < 0xfff) { return readUTF(); } else { return readSimpleString().toString(); } }
@Override public synchronized void saveBuffer(final OutputStream output) throws ActiveMQException { if (streamClosed) { throw ActiveMQClientMessageBundle.BUNDLE.largeMessageLostSession(); } setOutputStream(output); waitCompletion(0); }
@Test public void testErrorOnSetStreaming() throws Exception { long start = System.currentTimeMillis(); final LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 5, 30000); outBuffer.addPacket(new byte[]{0, 1, 2, 3, 4}, 1, true); final CountDownLatch latchBytesWritten1 = new CountDownLatch(5); final CountDownLatch latchBytesWritten2 = new CountDownLatch(10); outBuffer.setOutputStream(new OutputStream() { @Override public void write(final int b) throws IOException { latchBytesWritten1.countDown(); latchBytesWritten2.countDown(); } }); ActiveMQTestBase.waitForLatch(latchBytesWritten1); try { outBuffer.readByte(); Assert.fail("supposed to throw an exception"); } catch (IllegalAccessError ignored) { } Assert.assertTrue("It waited too much", System.currentTimeMillis() - start < 30000); }
long callTimeout = locator.getCallTimeout(); currentLargeMessageController = new LargeMessageControllerImpl(this, largeMessage.getLargeMessageSize(), callTimeout, largeMessageCache); currentLargeMessageController.setLocal(true); currentLargeMessageController.addPacket(body, body.length, false);
@Test public void testSplitBufferOnFile() throws Exception { LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 1024 * 1024, 1, getTestFile(), 1024); try { long count = 0; for (int i = 0; i < 10; i++) { byte[] buffer = new byte[10240]; for (int j = 0; j < 10240; j++) { buffer[j] = getSamplebyte(count++); } outBuffer.addPacket(buffer, 1, true); } outBuffer.readerIndex(0); for (int i = 0; i < 10240 * 10; i++) { assertEquals("position " + i, getSamplebyte(i), outBuffer.readByte()); } outBuffer.readerIndex(0); for (int i = 0; i < 10240 * 10; i++) { assertEquals("position " + i, getSamplebyte(i), outBuffer.readByte()); } } finally { outBuffer.close(); } }
@Test public void testStreamDataWaitCompletionOnInCompleteBuffer() throws Exception { final LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 5, 1000); class FakeOutputStream extends OutputStream { @Override public void write(int b) throws IOException { } } outBuffer.setOutputStream(new FakeOutputStream()); long time = System.currentTimeMillis(); try { outBuffer.waitCompletion(0); fail("supposed to throw an exception"); } catch (ActiveMQException e) { } assertTrue("It was supposed to wait at least 1 second", System.currentTimeMillis() - time > 1000); }
@Test public void testReadPartialData() throws Exception { final LargeMessageControllerImpl buffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 10, 10); buffer.addPacket(new byte[]{0, 1, 2, 3, 4}, 1, true); buffer.readBytes(bytes, 0, 5); buffer.cancel();
@Test public void testStreamData() throws Exception { final LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 1024 * 11 + 123, 1000); outBuffer.addPacket(new byte[1024], 1, true); outBuffer.setOutputStream(output); outBuffer.addPacket(new byte[1024], 1, true); outBuffer.addPacket(new byte[123], 1, false);
@Test public void testReadDataOverCached() throws Exception { clearDataRecreateServerDirs(); 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(), getTestFile()); Assert.assertEquals(str1, readBuffer.readUTF()); Assert.assertEquals(str2, readBuffer.readString()); Assert.assertEquals(d1, readBuffer.readDouble(), 0.00000001); Assert.assertEquals(f1, readBuffer.readFloat(), 0.000001); readBuffer.readerIndex(0); Assert.assertEquals(str1, readBuffer.readUTF()); Assert.assertEquals(str2, readBuffer.readString()); Assert.assertEquals(d1, readBuffer.readDouble(), 0.00000001); Assert.assertEquals(f1, readBuffer.readFloat(), 0.000001); readBuffer.close(); }
/** * from {@link java.io.DataInput} interface */ @Override public void readFully(byte[] b) throws IOException { readBytes(b); }
private LargeMessageControllerImpl splitBuffer(final int splitFactor, final byte[] bytes, final File file) throws Exception { LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), bytes.length, 5000, file); ByteArrayInputStream input = new ByteArrayInputStream(bytes); while (true) { byte[] splitElement = new byte[splitFactor]; int size = input.read(splitElement); if (size <= 0) { break; } if (size < splitFactor) { byte[] newSplit = new byte[size]; System.arraycopy(splitElement, 0, newSplit, 0, size); outBuffer.addPacket(newSplit, 1, input.available() > 0); } else { outBuffer.addPacket(splitElement, 1, input.available() > 0); } } return outBuffer; }
@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); }
@Test public void testReadLongs() throws Exception { LargeMessageControllerImpl buffer = createBufferWithLongs(3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); for (int i = 1; i <= 15; i++) { Assert.assertEquals(i, buffer.readLong()); } try { buffer.readByte(); Assert.fail("supposed to throw an exception"); } catch (IndexOutOfBoundsException e) { } }
@Override public double readDouble() { return Double.longBitsToDouble(readLong()); }
@Override public synchronized void handleLargeMessage(final ClientLargeMessageInternal clientLargeMessage, long largeMessageSize) throws Exception { if (closing) { // This is ok - we just ignore the message return; } // Flow control for the first packet, we will have others File largeMessageCache = null; if (session.isCacheLargeMessageClient()) { largeMessageCache = File.createTempFile("tmp-large-message-" + clientLargeMessage.getMessageID() + "-", ".tmp"); largeMessageCache.deleteOnExit(); } ClientSessionFactory sf = session.getSessionFactory(); ServerLocator locator = sf.getServerLocator(); long callTimeout = locator.getCallTimeout(); currentLargeMessageController = new LargeMessageControllerImpl(this, largeMessageSize, callTimeout, largeMessageCache); if (clientLargeMessage.isCompressed()) { clientLargeMessage.setLargeMessageController(new CompressedLargeMessageControllerImpl(currentLargeMessageController)); } else { clientLargeMessage.setLargeMessageController(currentLargeMessageController); } handleRegularMessage(clientLargeMessage); }