private String parseBody(byte[] data, int start, StompFrame frame) throws StompParseException { String strLenght = frame.getHeaderValue("content-length"); int bodySize = -1; if (strLenght != null) { bodySize = Integer.parseInt(strLenght); } String body; if (bodySize != -1) { body = copyBody(data, start, start + bodySize); } else { body = copyBody(data, start); } return body; }
public StompFrame parse(byte[] data) throws StompParseException { if (data == null) throw new IllegalArgumentException("data must not be NULL!"); StompFrame frame = null; int p = 0; boolean inCommand = true; boolean inHeader = false; boolean previousWasEOL = false; boolean bodyStart = false; FrameType type = null; try { for (int i = 0; i <= data.length; i++) { boolean consumed = false; if (data[i] == StompFrame.PROTOCOL_EOL) { // Skip CR after a LF if (data.length > i + 1 && data[i + 1] == '\r') { i++; } if (!previousWasEOL) { previousWasEOL = true; // Command or header end here if (inCommand) { type = parseFrameType(data, p, i); frame = new StompFrame(type); inCommand = false; inHeader = true; consumed = true; } else if (inHeader) { Pair<String> header = parseHeader(data, p, i); frame.withHeader(header.right, header.left); inHeader = true; consumed = true; } else { } } else { // Two EOLs - Body starts after this OR we are done p++; // Skip this EOL String body = parseBody(data, p, frame); if (body != null) { frame.withBody(body); } break; } } else if (data[i] == StompFrame.PROTOCOL_END) { previousWasEOL = false; break; } else { previousWasEOL = false; } if (consumed) { p = i + 1; } } } catch (Throwable cause) { throw new StompParseException( "Can not parse message:" + System.lineSeparator() + new String(data, encoding), cause); } return frame; }