public static void assertEqualsBuffers( final int size, final HornetQBuffer expected, final HornetQBuffer actual) { // assertEquals(expected.length, actual.length); expected.readerIndex(0); actual.readerIndex(0); for (int i = 0; i < size; i++) { byte b1 = expected.readByte(); byte b2 = actual.readByte(); Assert.assertEquals("byte at index " + i, b1, b2); } expected.resetReaderIndex(); actual.resetReaderIndex(); }
public void decodeHeadersAndProperties(final HornetQBuffer buffer) { messageID = buffer.readLong(); address = buffer.readNullableSimpleString(); if (buffer.readByte() == DataConstants.NOT_NULL) { byte[] bytes = new byte[16]; buffer.readBytes(bytes); userID = new UUID(UUID.TYPE_TIME_BASED, bytes); } else { userID = null; } type = buffer.readByte(); durable = buffer.readBoolean(); expiration = buffer.readLong(); timestamp = buffer.readLong(); priority = buffer.readByte(); properties.decode(buffer); }
/** * Large message version of {@link #assertMessageBody(int, ClientMessage)}. * * @param i * @param message */ protected static void assertLargeMessageBody(final int i, final ClientMessage message) { HornetQBuffer buffer = message.getBodyBuffer(); for (int j = 0; j < LARGE_MESSAGE_SIZE; j++) { Assert.assertTrue( "msg " + i + ", expecting " + LARGE_MESSAGE_SIZE + " bytes, got " + j, buffer.readable()); Assert.assertEquals("equal at " + j, UnitTestCase.getSamplebyte(j), buffer.readByte()); } }
protected String readLine(HornetQBuffer in, int maxLength, String errorMessage) throws IOException { byte b; ByteArrayOutputStream baos = new ByteArrayOutputStream(maxLength); while ((b = in.readByte()) != '\n') { if (baos.size() > maxLength) { throw new StompException(errorMessage, true); } baos.write(b); } byte[] sequence = baos.toByteArray(); return new String(sequence, "UTF-8"); }
@Override public void decodeRest(final HornetQBuffer buffer) { synchronizationIsFinished = buffer.readBoolean(); allowsAutoFailBack = buffer.readBoolean(); nodeID = buffer.readString(); if (synchronizationIsFinished) { return; } dataType = SyncDataType.getDataType(buffer.readByte()); int length = buffer.readInt(); ids = new long[length]; for (int i = 0; i < length; i++) { ids[i] = buffer.readLong(); } }
public synchronized List<PagedMessage> read(StorageManager storage) throws Exception { if (isDebug) { HornetQServerLogger.LOGGER.debug( "reading page " + this.pageId + " on address = " + storeName); } if (!file.isOpen()) { throw HornetQMessageBundle.BUNDLE.invalidPageIO(); } ArrayList<PagedMessage> messages = new ArrayList<PagedMessage>(); size.set((int) file.size()); // Using direct buffer, as described on https://jira.jboss.org/browse/HORNETQ-467 ByteBuffer directBuffer = storage.allocateDirectBuffer((int) file.size()); try { file.position(0); file.read(directBuffer); directBuffer.rewind(); HornetQBuffer fileBuffer = HornetQBuffers.wrappedBuffer(directBuffer); fileBuffer.writerIndex(fileBuffer.capacity()); while (fileBuffer.readable()) { final int position = fileBuffer.readerIndex(); byte byteRead = fileBuffer.readByte(); if (byteRead == Page.START_BYTE) { if (fileBuffer.readerIndex() + DataConstants.SIZE_INT < fileBuffer.capacity()) { int messageSize = fileBuffer.readInt(); int oldPos = fileBuffer.readerIndex(); if (fileBuffer.readerIndex() + messageSize < fileBuffer.capacity() && fileBuffer.getByte(oldPos + messageSize) == Page.END_BYTE) { PagedMessage msg = new PagedMessageImpl(); msg.decode(fileBuffer); byte b = fileBuffer.readByte(); if (b != Page.END_BYTE) { // Sanity Check: This would only happen if there is a bug on decode or any internal // code, as // this // constraint was already checked throw new IllegalStateException( "Internal error, it wasn't possible to locate END_BYTE " + b); } msg.initMessage(storage); if (isTrace) { HornetQServerLogger.LOGGER.trace( "Reading message " + msg + " on pageId=" + this.pageId + " for address=" + storeName); } messages.add(msg); } else { markFileAsSuspect(file.getFileName(), position, messages.size()); break; } } } else { markFileAsSuspect(file.getFileName(), position, messages.size()); break; } } } finally { storage.freeDirectBuffer(directBuffer); } numberOfMessages.set(messages.size()); return messages; }
public StompFrame unmarshal(HornetQBuffer in) throws IOException { try { String action = null; // skip white space to next real action line while (true) { action = readLine(in, MAX_COMMAND_LENGTH, "The maximum command length was exceeded"); if (action == null) { throw new IOException("connection was closed"); } else { action = action.trim(); if (action.length() > 0) { break; } } } // Parse the headers HashMap headers = new HashMap(25); while (true) { String line = readLine(in, MAX_HEADER_LENGTH, "The maximum header length was exceeded"); if (line != null && line.trim().length() > 0) { if (headers.size() > MAX_HEADERS) { throw new StompException("The maximum number of headers was exceeded", true); } try { int seperator_index = line.indexOf(Stomp.Headers.SEPERATOR); String name = line.substring(0, seperator_index).trim(); String value = line.substring(seperator_index + 1, line.length()).trim(); headers.put(name, value); } catch (Exception e) { throw new StompException("Unable to parser header line [" + line + "]", true); } } else { break; } } // Read in the data part. byte[] data = NO_DATA; String contentLength = (String) headers.get(Stomp.Headers.CONTENT_LENGTH); if (contentLength != null) { // Bless the client, he's telling us how much data to read in. int length; try { length = Integer.parseInt(contentLength.trim()); } catch (NumberFormatException e) { throw new StompException("Specified content-length is not a valid integer", true); } if (length > MAX_DATA_LENGTH) { throw new StompException("The maximum data length was exceeded", true); } data = new byte[length]; in.readBytes(data); if (in.readByte() != 0) { throw new StompException( Stomp.Headers.CONTENT_LENGTH + " bytes were read and " + "there was no trailing null byte", true); } } else { // We don't know how much to read.. data ends when we hit a 0 byte b; ByteArrayOutputStream baos = null; while (in.readableBytes() > 0 && (b = in.readByte()) != 0) { if (baos == null) { baos = new ByteArrayOutputStream(); } else if (baos.size() > MAX_DATA_LENGTH) { throw new StompException("The maximum data length was exceeded", true); } baos.write(b); } if (baos != null) { baos.close(); data = baos.toByteArray(); } } return new StompFrame(action, headers, data); } catch (StompException e) { return new StompFrameError(e); } }