/** * Tests that complete messages are delivered even when the message arrives one byte at a time * from the network. Then tests that a second message can still be received all-at-once. */ public void testPartialOneByteReceives() { int len = 32; byte[] expected = getByteSequence(len); ByteBuffer buf = ByteBuffer.allocate(len + 4, false); buf.putInt(len); buf.put(expected); buf = buf.asReadOnlyBuffer(); buf.flip(); for (; buf.hasRemaining(); buf.skip(1)) { assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); ByteBuffer singleByte = buf.slice().limit(1); harness.recv(singleByte); } assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); byte[] actual = receivedMessages.get(0); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); harness.recv(buf.rewind()); assertEquals(0, sentMessages.size()); assertEquals(2, receivedMessages.size()); actual = receivedMessages.get(1); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); }
/** * 向ByteBuffer中写入PropertyBag * * @param buf 采用mina的ByteBuffer,因为这个支持自动增长 */ public void writeBuffer(ByteBuffer buf) { short fieldCount = (short) valueMap.size(); buf.putShort(fieldCount); for (Map.Entry<Integer, Value<?>> entry : valueMap.entrySet()) { buf.putInt(entry.getKey()); Value<?> value = entry.getValue(); buf.put((byte) value.type.ordinal()); ProtocolUtils.putValueIntoPkt(buf, value.type, value.value); } }
/** Tests handling of a valid message with payload length zero. */ public void testReceiveValidZeroLength() { sizeBuf.putInt(0).flip(); harness.recv(sizeBuf.asReadOnlyBuffer()); assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); assertEquals(0, receivedMessages.get(0).length); }
protected static String readString(String charset, ByteBuffer buffer) { short len = buffer.getShort(); byte[] bytes = new byte[len]; buffer.get(bytes); try { return new String(bytes, charset); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } }
@Override public ByteBuffer encodedContent() { ByteBuffer ret = ByteBuffer.allocate(32); ret.setAutoExpand(true); User.writeTo(user, ret); ret.flip(); return ret; }
/** * 从ByteBuffer解析得到PropertyBag * * @param buf 采用mina的ByteBuffer,因为这个支持自动增长 * @return */ public static PropertyBag readBuffer(ByteBuffer buf) { PropertyBag bag = new PropertyBag(); short fieldCount = buf.getShort(); for (int i = 0; i < fieldCount; i++) { int property = buf.getInt(); ValueType type = getTypeCode(buf.get()); Object val = ProtocolUtils.readValueFromPkt(buf, type); bag.put(property, val); } return bag; }
/** * Get the remaining bytes that could be read from a file or ByteBuffer. * * @return Number of remaining bytes */ private long getRemainingBytes() { if (!useLoadBuf) { return in.remaining(); } try { return channelSize - channel.position() + in.remaining(); } catch (Exception e) { log.error("Error getRemainingBytes", e); return 0; } }
void check() throws JMSException, AMQFrameDecodingException { for (Object m : received) { ByteBuffer buffer = ((JMSBytesMessage) m).getData(); FieldTable actual = FieldTableFactory.newFieldTable(buffer, buffer.remaining()); for (String key : _expected.keys()) { assertEquals( "Values for " + key + " did not match", _expected.getObject(key), actual.getObject(key)); } } }
/** {@inheritDoc} */ public void decodeHeader() { // XXX check signature? // SIGNATURE, lets just skip fillBuffer(9); header = new FLVHeader(); in.skip(3); header.setVersion(in.get()); header.setTypeFlags(in.get()); header.setDataOffset(in.getInt()); if (log.isDebugEnabled()) { log.debug("Header: " + header.toString()); } }
private static void writeString(String str, ByteBuffer buffer, String charset) { try { if (str == null) { str = ""; } if (buffer != null) { byte[] bytes = str.getBytes(charset); buffer.putShort((short) bytes.length); buffer.put(bytes); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
/** * 转换为字节数组,包括长度 * * @return */ public byte[] getByties() { int size = countSize(); ByteBuffer buffer = ByteBuffer.allocate(size); writeBuffer(buffer); buffer.flip(); byte[] ret = new byte[size]; buffer.get(ret); return ret; }
/** * 字节数组序列化成PropertyBag * * @param byties * @return */ public static PropertyBag valueOfByties(byte[] byties) { if (byties == null || byties.length < 2) { return new PropertyBag(); } return readBuffer(ByteBuffer.wrap(byties)); }
private void sendMessages(int messageCount, boolean persistent) throws AMQException { for (int i = 0; i < messageCount; i++) { IncomingMessage currentMessage = message(false, persistent); ArrayList<AMQQueue> qs = new ArrayList<AMQQueue>(); qs.add(getQueue()); currentMessage.enqueue(qs); // route header MessageMetaData mmd = currentMessage.headersReceived(); currentMessage.setStoredMessage(getMessageStore().addMessage(mmd)); // Add the body so we have somthing to test later currentMessage.addContentBodyFrame( getSession() .getMethodRegistry() .getProtocolVersionMethodConverter() .convertToContentChunk( new ContentBody(ByteBuffer.allocate((int) MESSAGE_SIZE), MESSAGE_SIZE))); AMQMessage m = new AMQMessage(currentMessage.getStoredMessage()); for (BaseQueue q : currentMessage.getDestinationQueues()) { q.enqueue(m); } } }
public static int readInt(ByteBuffer buffer) { if (buffer != null) { return buffer.getInt(); } else { return 0; } }
public static short readShort(ByteBuffer buffer) { if (buffer != null) { return buffer.getShort(); } else { return 0; } }
public static Double readDouble(ByteBuffer buffer) { if (buffer != null) { return buffer.getDouble(); } else { return 0.0d; } }
/** Performs cleanup after each test case. */ @Override public void tearDown() { sizeBuf.clear(); receivedMessages.clear(); sentMessages.clear(); harness = null; }
public static Long readLong(ByteBuffer buffer) { if (buffer != null) { return buffer.getLong(); } else { return 0L; } }
public static Float readFloat(ByteBuffer buffer) { if (buffer != null) { return buffer.getFloat(); } else { return 0f; } }
/** * Sets up a {@link FilterTestHarness} for this test, and clears the list of received and sent * messages. */ @Override public void setUp() { System.err.println("Testcase: " + this.getName()); sizeBuf.sweep(); receivedMessages.clear(); sentMessages.clear(); harness = new FilterTestHarness(this); }
/** Tests that a big message gets reassembled by the filter. */ public void testBigReceive() { // Send a 62KB message in 1KB chunks int len = 62 * 1024; byte[] expected = new byte[len]; sizeBuf.putInt(len).flip(); harness.recv(sizeBuf.asReadOnlyBuffer()); ByteBuffer buf = ByteBuffer.wrap(getByteSequence(1024)).asReadOnlyBuffer(); // Do the recvs for (int i = 0; i < 62; ++i) { assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); buf.rewind(); buf.get(expected, i * 1024, 1024); buf.rewind(); harness.recv(buf); } assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); byte[] actual = receivedMessages.get(0); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); }
/** Tests handling of exceptions during dispatch. */ public void testReceiveHandlingException() { int len = 1000; byte[] expected = getByteSequence(len); ByteBuffer buf = ByteBuffer.allocate(len + 4, false); buf.putInt(len); buf.put(expected); buf = buf.asReadOnlyBuffer(); buf.flip(); assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); RuntimeException expectedEx = new RuntimeException("Dummy exception for testing filter recv"); harness.setExceptionOnNextCompleteMessage(expectedEx); // This recv will fail to process the message harness.recv(buf); assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); // Send a second message, expecting the first to have been dropped. buf.rewind(); harness.recv(buf); assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); }
/** Tests that two simple receives work. */ public void testMultipleSimpleReceives() { int len = 1000; byte[] expected = getByteSequence(len); ByteBuffer buf = ByteBuffer.allocate(len + 4, false); buf.putInt(len); buf.put(expected); buf = buf.asReadOnlyBuffer(); buf.flip(); assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); harness.recv(buf); assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); byte[] actual = receivedMessages.get(0); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); harness.recv(buf.rewind()); assertEquals(0, sentMessages.size()); assertEquals(2, receivedMessages.size()); actual = receivedMessages.get(1); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); }
/** Creates a new instance of <code>MinaClientManager</code> */ public MinaClientManager() throws UnknownHostException { readProperties(); logProperties(); ByteBuffer.setUseDirectBuffers(false); ByteBuffer.setAllocator(new SimpleByteBufferAllocator()); connector = new SocketConnector(); cfg = new SocketConnectorConfig(); cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CTPProtocolCodecFactory())); cfg.getFilterChain().addLast("logger", new LoggingFilter()); cfg.setConnectTimeout(30); sessions = new ArrayList(); }
/** * Get the current position in a file or ByteBuffer. * * @return Current position in a file */ private long getCurrentPosition() { long pos; if (!useLoadBuf) { return in.position(); } try { if (in != null) { pos = (channel.position() - in.remaining()); } else { pos = channel.position(); } return pos; } catch (Exception e) { log.error("Error getCurrentPosition", e); return 0; } }
/** * Read only header part of a tag. * * @return Tag header */ private ITag readTagHeader() { // PREVIOUS TAG SIZE fillBuffer(15); int previousTagSize = in.getInt(); // START OF FLV TAG byte dataType = in.get(); // The next two lines use a utility method which reads in // three consecutive bytes but stores them in a 4 byte int. // We are able to write those three bytes back out by using // another utility method which strips off the last byte // However, we will have to check into this during optimization. int bodySize = IOUtils.readUnsignedMediumInt(in); int timestamp = IOUtils.readUnsignedMediumInt(in); // reserved in.getInt(); return new Tag(dataType, timestamp, bodySize, null, previousTagSize); }
/** * Modifies current position. * * @param pos Current position in file */ private void setCurrentPosition(long pos) { if (pos == Long.MAX_VALUE) { pos = file.length(); } if (!useLoadBuf) { in.position((int) pos); return; } try { if (pos >= (channel.position() - in.limit()) && pos < channel.position()) { in.position((int) (pos - (channel.position() - in.limit()))); } else { channel.position(pos); fillBuffer(bufferSize, true); } } catch (Exception e) { log.error("Error setCurrentPosition", e); } }
public int getAudioCodecId() { KeyFrameMeta meta = analyzeKeyFrames(); if (meta == null) return -1; long old = getCurrentPosition(); setCurrentPosition(firstAudioTag); readTagHeader(); fillBuffer(1); byte frametype = in.get(); setCurrentPosition(old); return frametype & MASK_SOUND_FORMAT; }
public int getVideoCodecId() { KeyFrameMeta meta = analyzeKeyFrames(); if (meta == null) return -1; long old = getCurrentPosition(); setCurrentPosition(firstVideoTag); readTagHeader(); fillBuffer(1); byte frametype = in.get(); setCurrentPosition(old); return frametype & MASK_VIDEO_CODEC; }
/** * Get the total readable bytes in a file or ByteBuffer. * * @return Total readable bytes */ private long getTotalBytes() { if (!useLoadBuf) { return in.capacity(); } try { return channelSize; } catch (Exception e) { log.error("Error getTotalBytes", e); return 0; } }