@Override public void beforeMarshall(WireFormat wireFormat) throws IOException { // Need to marshal the properties. if (marshalledProperties == null && properties != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); MarshallingSupport.marshalPrimitiveMap(properties, os); os.close(); marshalledProperties = baos.toByteSequence(); } }
DataOutputStream dataOut = new DataOutputStream(writeBuffer); headerMarshaller.writeHeader(command, dataOut); int offset = writeBuffer.size(); } else { byte[] data = writeBuffer.toByteArray(); boolean lastFragment = false; int length = data.length; dataOut.writeInt(chunkSize); chunkSize -= 4; dataOut.write(LastPartialCommand.DATA_STRUCTURE_TYPE); } else { dataOut.write(PartialCommand.DATA_STRUCTURE_TYPE);
protected byte[] decompress(ByteSequence dataSequence) throws IOException { Inflater inflater = new Inflater(); ByteArrayOutputStream decompressed = new ByteArrayOutputStream(); try { length = ByteSequenceData.readIntBig(dataSequence); dataSequence.offset = 0; byte[] data = Arrays.copyOfRange(dataSequence.getData(), 4, dataSequence.getLength()); inflater.setInput(data); byte[] buffer = new byte[length]; int count = inflater.inflate(buffer); decompressed.write(buffer, 0, count); return decompressed.toByteArray(); } catch (Exception e) { throw new IOException(e); } finally { inflater.end(); decompressed.close(); } }
protected void doCompress() throws IOException { compressed = true; ByteSequence bytes = getContent(); ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); OutputStream os = new DeflaterOutputStream(bytesOut); os.write(bytes.data, bytes.offset, bytes.length); os.close(); setContent(bytesOut.toByteSequence()); }
@Override public void storeContent() { try { if (getContent() == null && !map.isEmpty()) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { compressed = true; os = new DeflaterOutputStream(os); } DataOutputStream dataOut = new DataOutputStream(os); MarshallingSupport.marshalPrimitiveMap(map, dataOut); dataOut.close(); setContent(bytesOut.toByteSequence()); } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void storeContent() { try { ByteSequence content = getContent(); String text = this.text; if (content == null && text != null) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { compressed = true; os = new DeflaterOutputStream(os); } DataOutputStream dataOut = new DataOutputStream(os); MarshallingSupport.writeUTF8(dataOut, text); dataOut.close(); setContent(bytesOut.toByteSequence()); } } catch (IOException e) { throw new RuntimeException(e); } }
ActiveMQTextMessage text = new ActiveMQTextMessage(); try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(command.getContent().length + 4); DataOutputStream data = new DataOutputStream(bytes); data.writeInt(command.getContent().length); data.write(command.getContent()); text.setContent(bytes.toByteSequence()); data.close(); } catch (Throwable e) { ActiveMQBytesMessage byteMessage = new ActiveMQBytesMessage(); byteMessage.writeBytes(command.getContent()); msg = byteMessage; } else { ActiveMQBytesMessage bm = new ActiveMQBytesMessage(); bm.writeBytes(command.getContent()); msg = bm; ActiveMQTextMessage text = new ActiveMQTextMessage(); try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(command.getContent().length + 4); DataOutputStream data = new DataOutputStream(bytes); data.writeInt(command.getContent().length); data.write(command.getContent()); text.setContent(bytes.toByteSequence()); data.close(); } catch (Throwable e) {
private void trace(DataStructure command) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(maxTraceDatagramSize); DataOutputStream out = new DataOutputStream(baos); wireFormat.marshal(brokerId, out); wireFormat.marshal(command, out); out.close(); ByteSequence sequence = baos.toByteSequence(); DatagramPacket datagram = new DatagramPacket(sequence.getData(), sequence.getOffset(), sequence.getLength(), address); socket.send(datagram); } catch (Throwable e) { LOG.debug("Failed to trace: {}", command, e); } }
@Override public void storeContent() { ByteSequence bodyAsBytes = getContent(); if (bodyAsBytes == null && object != null) { try { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { compressed = true; os = new DeflaterOutputStream(os); } DataOutputStream dataOut = new DataOutputStream(os); ObjectOutputStream objOut = new ObjectOutputStream(dataOut); objOut.writeObject(object); objOut.flush(); objOut.reset(); objOut.close(); setContent(bytesOut.toByteSequence()); } catch (IOException ioe) { throw new RuntimeException(ioe.getMessage(), ioe); } } }
private void initializeWriting() throws JMSException { checkReadOnlyBody(); if (this.dataOut == null) { this.bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; this.dataOut = new DataOutputStream(os); } restoreOldContent(); }
private void initializeWriting() throws JMSException { checkReadOnlyBody(); if (this.dataOut == null) { this.bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { compressed = true; os = new DeflaterOutputStream(os); this.dataOut = new DataOutputStream(os); try { if (compressed) { ByteArrayInputStream input = new ByteArrayInputStream(this.content.getData(), this.content.getOffset(), this.content.getLength()); InflaterInputStream inflater = new InflaterInputStream(input); try { int read = 0; while ((read = inflater.read(buffer)) != -1) { this.dataOut.write(buffer, 0, read); this.dataOut.write(this.content.getData(), this.content.getOffset(), this.content.getLength());
private void initializeWriting() throws JMSException { checkReadOnlyBody(); if (this.dataOut == null) { this.bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { compressed = true; final Deflater deflater = new Deflater(Deflater.BEST_SPEED); os = new FilterOutputStream(new DeflaterOutputStream(os, deflater)) { public void write(byte[] arg0) throws IOException { length += arg0.length; this.dataOut = new DataOutputStream(os);
@Override public void storeContent() { if (dataOut != null) { try { dataOut.close(); ByteSequence bs = bytesOut.toByteSequence(); setContent(bs); ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { doCompress(); } } catch (IOException ioe) { throw new RuntimeException(ioe.getMessage(), ioe); } finally { try { if (bytesOut != null) { bytesOut.close(); bytesOut = null; } if (dataOut != null) { dataOut.close(); dataOut = null; } } catch (IOException ioe) { } } } }
/** * Puts the message body in read-only mode and repositions the stream of * bytes to the beginning. * * @throws JMSException if an internal error occurs */ @Override public void reset() throws JMSException { storeContent(); setReadOnlyBody(true); try { if (bytesOut != null) { bytesOut.close(); bytesOut = null; } if (dataIn != null) { dataIn.close(); dataIn = null; } if (dataOut != null) { dataOut.close(); dataOut = null; } } catch (IOException ioe) { throw JMSExceptionSupport.create(ioe); } }
public void write(Command command, SocketAddress address) throws IOException { synchronized (writeLock) { ByteArrayOutputStream largeBuffer = new ByteArrayOutputStream(defaultMarshalBufferSize); wireFormat.marshal(command, new DataOutputStream(largeBuffer)); byte[] data = largeBuffer.toByteArray(); int size = data.length;
@Override public void storeContent() { if (dataOut != null) { try { dataOut.close(); setContent(bytesOut.toByteSequence()); bytesOut = null; dataOut = null; } catch (IOException ioe) { throw new RuntimeException(ioe); } } }
private void storeContent() { try { if (dataOut != null) { dataOut.close(); ByteSequence bs = bytesOut.toByteSequence(); if (compressed) { int pos = bs.offset; ByteSequenceData.writeIntBig(bs, length); bs.offset = pos; } setContent(bs); bytesOut = null; dataOut = null; } } catch (IOException ioe) { throw new RuntimeException(ioe.getMessage(), ioe); // TODO verify // RuntimeException } }
private ByteSequence readHeaderLine(DataInput in, int maxLength, String errorMessage) throws IOException { byte b; ByteArrayOutputStream baos = new ByteArrayOutputStream(maxLength); while ((b = in.readByte()) != '\n') { if (baos.size() > maxLength) { baos.close(); throw new ProtocolException(errorMessage, true); } baos.write(b); } baos.close(); ByteSequence line = baos.toByteSequence(); if (stompVersion.equals(Stomp.V1_0) || stompVersion.equals(Stomp.V1_2)) { int lineLength = line.getLength(); if (lineLength > 0 && line.data[lineLength-1] == '\r') { line.setLength(lineLength-1); } } return line; }
@Override protected void doCompress() throws IOException { compressed = true; ByteSequence bytes = getContent(); if (bytes != null) { int length = bytes.getLength(); ByteArrayOutputStream compressed = new ByteArrayOutputStream(); compressed.write(new byte[4]); Deflater deflater = new Deflater(); try { deflater.setInput(bytes.data); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); compressed.write(buffer, 0, count); } bytes = compressed.toByteSequence(); ByteSequenceData.writeIntBig(bytes, length); bytes.offset = 0; setContent(bytes); } finally { deflater.end(); compressed.close(); } } } }
private String readLine(DataInput 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 ProtocolException(errorMessage, true); } baos.write(b); } baos.close(); ByteSequence sequence = baos.toByteSequence(); return new String(sequence.getData(), sequence.getOffset(), sequence.getLength(), "UTF-8"); }