コード例 #1
0
  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()) {
        // keep track of the real length of the content if
        // we are compressed.
        try {
          os.write(new byte[4]);
        } catch (IOException e) {
          throw JMSExceptionSupport.create(e);
        }
        length = 0;
        compressed = true;
        final Deflater deflater = new Deflater(Deflater.BEST_SPEED);
        os =
            new FilterOutputStream(new DeflaterOutputStream(os, deflater)) {
              @Override
              public void write(byte[] arg0) throws IOException {
                length += arg0.length;
                out.write(arg0);
              }

              @Override
              public void write(byte[] arg0, int arg1, int arg2) throws IOException {
                length += arg2;
                out.write(arg0, arg1, arg2);
              }

              @Override
              public void write(int arg0) throws IOException {
                length++;
                out.write(arg0);
              }

              @Override
              public void close() throws IOException {
                super.close();
                deflater.end();
              }
            };
      }
      this.dataOut = new DataOutputStream(os);
    }
  }