コード例 #1
0
ファイル: GzipSink.java プロジェクト: dyglcc/mmwd
  @Override
  public void close() throws IOException {
    if (closed) return;

    // This method delegates to the DeflaterSink for finishing the deflate process
    // but keeps responsibility for releasing the deflater's resources. This is
    // necessary because writeFooter needs to query the proccessed byte count which
    // only works when the defalter is still open.

    Throwable thrown = null;
    try {
      deflaterSink.finishDeflate();
      writeFooter();
    } catch (Throwable e) {
      thrown = e;
    }

    try {
      deflater.end();
    } catch (Throwable e) {
      if (thrown == null) thrown = e;
    }

    try {
      sink.close();
    } catch (Throwable e) {
      if (thrown == null) thrown = e;
    }
    closed = true;

    if (thrown != null) Util.sneakyRethrow(thrown);
  }
コード例 #2
0
  public ZipRepository(final InputStream in, final MimeRegistry mimeRegistry) throws IOException {
    this(mimeRegistry);

    final ZipInputStream zipIn = new ZipInputStream(in);
    try {
      ZipEntry nextEntry = zipIn.getNextEntry();
      while (nextEntry != null) {
        final String[] buildName = RepositoryUtilities.splitPath(nextEntry.getName(), "/");
        if (nextEntry.isDirectory()) {
          root.updateDirectoryEntry(buildName, 0, nextEntry);
        } else {
          final ByteArrayOutputStream bos = new ByteArrayOutputStream();
          final Deflater def = new Deflater(nextEntry.getMethod());
          try {
            final DeflaterOutputStream dos = new DeflaterOutputStream(bos, def);
            try {
              IOUtils.getInstance().copyStreams(zipIn, dos);
              dos.flush();
            } finally {
              dos.close();
            }
          } finally {
            def.end();
          }

          root.updateEntry(buildName, 0, nextEntry, bos.toByteArray());
        }

        zipIn.closeEntry();
        nextEntry = zipIn.getNextEntry();
      }
    } finally {
      zipIn.close();
    }
  }
コード例 #3
0
 /**
  * deflater 壓縮
  *
  * @param value
  * @return
  */
 public static byte[] deflater(byte[] value) {
   byte[] result = new byte[0];
   Deflater deflater = null;
   ByteArrayOutputStream out = null;
   //
   try {
     deflater = new Deflater();
     deflater.setLevel(Deflater.BEST_SPEED); // fast
     deflater.setInput(value);
     deflater.finish();
     //
     out = new ByteArrayOutputStream();
     byte[] buff = new byte[1024];
     while (!deflater.finished()) {
       int count = deflater.deflate(buff);
       out.write(buff, 0, count);
     }
     //
     result = out.toByteArray();
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (deflater != null) {
       deflater.end(); // 需end,不然會有oom(約5000次時)
     }
     IoHelper.close(out);
   }
   return result;
 }
コード例 #4
0
 /**
  * {@collect.stats} {@description.open} Writes remaining compressed data to the output stream and
  * closes the underlying stream. {@description.close}
  *
  * @exception IOException if an I/O error has occurred
  */
 public void close() throws IOException {
   if (!closed) {
     finish();
     if (usesDefaultDeflater) def.end();
     out.close();
     closed = true;
   }
 }
コード例 #5
0
 private void deflate() {
   Deflater deflater = new Deflater(-1);
   try {
     deflater.setInput(this.field_149278_f, 0, this.field_149278_f.length);
     deflater.finish();
     byte[] deflated = new byte[this.field_149278_f.length];
     this.field_149285_h = deflater.deflate(deflated);
     this.field_149281_e = deflated;
   } finally {
     deflater.end();
   }
 }
コード例 #6
0
  /**
   * Closes this input stream and its underlying input stream, discarding any pending uncompressed
   * data.
   *
   * @throws IOException if an I/O error occurs
   */
  public void close() throws IOException {
    if (in != null) {
      try {
        // Clean up
        if (usesDefaultDeflater) {
          def.end();
        }

        in.close();
      } finally {
        in = null;
      }
    }
  }
コード例 #7
0
 public static byte[] compress(byte[] input) throws IOException {
   Deflater compressor = new Deflater();
   compressor.setLevel(Deflater.BEST_SPEED);
   compressor.setInput(input);
   compressor.finish();
   ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length / 10);
   byte[] buf = new byte[input.length / 10];
   while (!compressor.finished()) {
     int count = compressor.deflate(buf);
     bos.write(buf, 0, count);
   }
   bos.close();
   compressor.end();
   return bos.toByteArray();
 }
コード例 #8
0
 /**
  * Writes new sequence part entry.
  *
  * @param accession accession number of original sequence
  * @param from position of first nucleotide of content of this entry in the original sequence
  *     (e.g. 0 if this entry contains whole sequence)
  * @param sequence content of this entry
  * @throws java.io.IOException if an I/O error occurs.
  */
 public void writeSequencePart(
     String accession, int from, NucleotideSequence sequence, boolean compressed)
     throws IOException {
   stream.writeByte(compressed ? SEQUENCE_PART_ENTRY_TYPE_COMPRESSED : SEQUENCE_PART_ENTRY_TYPE);
   stream.writeUTF(accession);
   stream.writeInt(from);
   if (compressed) {
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     final Deflater def = new Deflater(9, true);
     DeflaterOutputStream dos = new DeflaterOutputStream(bos, def);
     SequencesUtils.convertNSequenceToBit2Array(sequence).writeTo(new DataOutputStream(dos));
     dos.close();
     def.end();
     stream.writeInt(bos.size());
     stream.write(bos.toByteArray());
   } else SequencesUtils.convertNSequenceToBit2Array(sequence).writeTo(stream);
 }
コード例 #9
0
ファイル: Deflater.java プロジェクト: hambroperks/j2objc
 @Override
 protected void finalize() {
   try {
     if (guard != null) {
       guard.warnIfOpen();
     }
     synchronized (this) {
       end(); // to allow overriding classes to clean up
       endImpl(); // in case those classes don't call super.end()
     }
   } finally {
     try {
       super.finalize();
     } catch (Throwable t) {
       throw new AssertionError(t);
     }
   }
 }
コード例 #10
0
ファイル: PRStream.java プロジェクト: JstnPwll/DroidText
 /**
  * Creates a new PDF stream object that will replace a stream in a existing PDF file.
  *
  * @param reader the reader that holds the existing PDF
  * @param conts the new content
  * @param compressionLevel the compression level for the content
  * @since 2.1.3 (replacing the existing constructor without param compressionLevel)
  */
 public PRStream(PdfReader reader, byte[] conts, int compressionLevel) {
   this.reader = reader;
   this.offset = -1;
   if (Document.compress) {
     try {
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       Deflater deflater = new Deflater(compressionLevel);
       DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
       zip.write(conts);
       zip.close();
       deflater.end();
       bytes = stream.toByteArray();
     } catch (IOException ioe) {
       throw new ExceptionConverter(ioe);
     }
     put(PdfName.FILTER, PdfName.FLATEDECODE);
   } else bytes = conts;
   setLength(bytes.length);
 }
コード例 #11
0
  public Packet51MapChunk(Chunk par1Chunk, boolean par2, int par3) {
    this.isChunkDataPacket = true;
    this.xCh = par1Chunk.xPosition;
    this.zCh = par1Chunk.zPosition;
    this.includeInitialize = par2;
    Packet51MapChunkData var4 = func_73594_a(par1Chunk, par2, par3);
    Deflater var5 = new Deflater(-1);
    this.yChMax = var4.field_74581_c;
    this.yChMin = var4.field_74580_b;

    try {
      this.field_73596_g = var4.field_74582_a;
      var5.setInput(var4.field_74582_a, 0, var4.field_74582_a.length);
      var5.finish();
      this.chunkData = new byte[var4.field_74582_a.length];
      this.tempLength = var5.deflate(this.chunkData);
    } finally {
      var5.end();
    }
  }
コード例 #12
0
ファイル: PRStream.java プロジェクト: JstnPwll/DroidText
 /**
  * Sets the data associated with the stream, either compressed or uncompressed. Note that the data
  * will never be compressed if Document.compress is set to false.
  *
  * @param data raw data, decrypted and uncompressed.
  * @param compress true if you want the stream to be compressed.
  * @param compressionLevel a value between -1 and 9 (ignored if compress == false)
  * @since iText 2.1.3
  */
 public void setData(byte[] data, boolean compress, int compressionLevel) {
   remove(PdfName.FILTER);
   this.offset = -1;
   if (Document.compress && compress) {
     try {
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       Deflater deflater = new Deflater(compressionLevel);
       DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
       zip.write(data);
       zip.close();
       deflater.end();
       bytes = stream.toByteArray();
       this.compressionLevel = compressionLevel;
     } catch (IOException ioe) {
       throw new ExceptionConverter(ioe);
     }
     put(PdfName.FILTER, PdfName.FLATEDECODE);
   } else bytes = data;
   setLength(bytes.length);
 }
コード例 #13
0
  private void deflate() {
    byte[] data = new byte[maxLen];
    int offset = 0;
    for (int x = 0; x < field_73584_f.length; x++) {
      System.arraycopy(field_73584_f[x], 0, data, offset, field_73584_f[x].length);
      offset += field_73584_f[x].length;
    }

    Deflater deflater = new Deflater(-1);

    try {
      deflater.setInput(data, 0, maxLen);
      deflater.finish();
      byte[] deflated = new byte[maxLen];
      this.dataLength = deflater.deflate(deflated);
      this.chunkDataBuffer = deflated;
    } finally {
      deflater.end();
    }
  }
コード例 #14
0
  @Test
  public void testDeflateBasics() throws Exception {
    // Setup deflater basics
    boolean nowrap = true;
    Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, nowrap);
    compressor.setStrategy(Deflater.DEFAULT_STRATEGY);

    // Text to compress
    String text = "info:";
    byte uncompressed[] = StringUtil.getUtf8Bytes(text);

    // Prime the compressor
    compressor.reset();
    compressor.setInput(uncompressed, 0, uncompressed.length);
    compressor.finish();

    // Perform compression
    ByteBuffer outbuf = ByteBuffer.allocate(64);
    BufferUtil.clearToFill(outbuf);

    while (!compressor.finished()) {
      byte out[] = new byte[64];
      int len = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
      if (len > 0) {
        outbuf.put(out, 0, len);
      }
    }
    compressor.end();

    BufferUtil.flipToFlush(outbuf, 0);
    byte b0 = outbuf.get(0);
    if ((b0 & 1) != 0) {
      outbuf.put(0, (b0 ^= 1));
    }
    byte compressed[] = BufferUtil.toArray(outbuf);

    String actual = TypeUtil.toHexString(compressed);
    String expected = "CaCc4bCbB70200"; // what pywebsocket produces

    Assert.assertThat("Compressed data", actual, is(expected));
  }
コード例 #15
0
  public Packet51MapChunk(Chunk chunk, boolean flag, int i) {
    this.lowPriority = true;
    this.a = chunk.x;
    this.b = chunk.z;
    this.e = flag;
    ChunkMap chunkmap = a(chunk, flag, i);
    Deflater deflater = new Deflater(-1);

    this.d = chunkmap.c;
    this.c = chunkmap.b;

    try {
      this.inflatedBuffer = chunkmap.a;
      deflater.setInput(chunkmap.a, 0, chunkmap.a.length);
      deflater.finish();
      this.buffer = new byte[chunkmap.a.length];
      this.size = deflater.deflate(this.buffer);
    } finally {
      deflater.end();
    }
  }
コード例 #16
0
ファイル: NetUtil.java プロジェクト: niczy/hyo
 /**
  * compress a byte array to a new byte array by {@link java.util.zip.Deflater}
  *
  * @param data input data to compress
  * @param len the 0..length in data is for compress
  * @return compressed byte array
  */
 public static byte[] deflateCompress(byte[] data) {
   Deflater compresser = new Deflater();
   ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
   try {
     compresser.reset();
     compresser.setInput(data, 0, data.length);
     compresser.finish();
     byte[] buf = new byte[1024];
     while (!compresser.finished()) {
       int i = compresser.deflate(buf);
       bos.write(buf, 0, i);
     }
     return bos.toByteArray();
   } finally {
     compresser.end();
     try {
       bos.close();
     } catch (IOException e) {
     }
   }
 }
コード例 #17
0
  public Packet56MapChunks(List p_i3324_1_) {
    int i = p_i3324_1_.size();
    field_73589_c = new int[i];
    field_73586_d = new int[i];
    field_73590_a = new int[i];
    field_73588_b = new int[i];
    field_73584_f = new byte[i][];
    int j = 0;
    for (int k = 0; k < i; k++) {
      Chunk chunk = (Chunk) p_i3324_1_.get(k);
      Packet51MapChunkData packet51mapchunkdata = Packet51MapChunk.func_73594_a(chunk, true, 65535);
      if (field_73591_h.length < j + packet51mapchunkdata.field_74582_a.length) {
        byte abyte0[] = new byte[j + packet51mapchunkdata.field_74582_a.length];
        System.arraycopy(field_73591_h, 0, abyte0, 0, field_73591_h.length);
        field_73591_h = abyte0;
      }
      System.arraycopy(
          packet51mapchunkdata.field_74582_a,
          0,
          field_73591_h,
          j,
          packet51mapchunkdata.field_74582_a.length);
      j += packet51mapchunkdata.field_74582_a.length;
      field_73589_c[k] = chunk.field_76635_g;
      field_73586_d[k] = chunk.field_76647_h;
      field_73590_a[k] = packet51mapchunkdata.field_74580_b;
      field_73588_b[k] = packet51mapchunkdata.field_74581_c;
      field_73584_f[k] = packet51mapchunkdata.field_74582_a;
    }

    Deflater deflater = new Deflater(-1);
    try {
      deflater.setInput(field_73591_h, 0, j);
      deflater.finish();
      field_73587_e = new byte[j];
      field_73585_g = deflater.deflate(field_73587_e);
    } finally {
      deflater.end();
    }
  }
コード例 #18
0
  /**
   * {@inheritDoc}
   *
   * @throws Zip64RequiredException if the archive's size exceeds 4 GByte or there are more than
   *     65535 entries inside the archive and {@link #setUseZip64} is {@link Zip64Mode#Never}.
   */
  @Override
  public void finish() throws IOException {
    if (finished) {
      throw new IOException("This archive has already been finished");
    }

    if (entry != null) {
      throw new IOException("This archive contains unclosed entries.");
    }

    cdOffset = written;
    for (ZipArchiveEntry ze : entries) {
      writeCentralFileHeader(ze);
    }
    cdLength = written - cdOffset;
    writeZip64CentralDirectory();
    writeCentralDirectoryEnd();
    offsets.clear();
    entries.clear();
    def.end();
    finished = true;
  }
コード例 #19
0
  public static byte[] compress(String data) {
    byte[] encodedData;
    try {
      encodedData = data.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      System.out.println("Compression error: UTF-8 is not supported. " + "Using default encoding.");
      encodedData = data.getBytes();
    }

    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    deflater.setInput(encodedData);
    deflater.finish();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    while (!deflater.finished()) {
      int byteCount = deflater.deflate(buf);
      baos.write(buf, 0, byteCount);
    }
    deflater.end();

    byte[] compressedData = baos.toByteArray();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      outputStream.write(ByteBuffer.allocate(4).putInt(encodedData.length).array());
    } catch (IOException e) {
      System.out.println("Compression error: Unable to write compressed data length.");
      e.printStackTrace();
    }
    try {
      outputStream.write(compressedData);
    } catch (IOException e) {
      System.out.println("Compression error: Unable to write compressed data.");
      e.printStackTrace();
    }

    return outputStream.toByteArray();
  }
コード例 #20
0
ファイル: Utils.java プロジェクト: beobal/cassandra-jdbc
  /**
   * Use the Compression object method to deflate the query string
   *
   * @param queryStr An un-compressed CQL query string
   * @param compression The compression object
   * @return A compressed string
   */
  public static ByteBuffer compressQuery(String queryStr, Compression compression) {
    byte[] data = queryStr.getBytes(Charsets.UTF_8);
    Deflater compressor = new Deflater();
    compressor.setInput(data);
    compressor.finish();

    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    try {
      while (!compressor.finished()) {
        int size = compressor.deflate(buffer);
        byteArray.write(buffer, 0, size);
      }
    } finally {
      compressor.end(); // clean up after the Deflater
    }

    logger.trace(
        "Compressed query statement {} bytes in length to {} bytes", data.length, byteArray.size());

    return ByteBuffer.wrap(byteArray.toByteArray());
  }
コード例 #21
0
  private static ActiveMQMessage toAMQMessage(
      MessageReference reference,
      ServerMessage coreMessage,
      WireFormat marshaller,
      ActiveMQDestination actualDestination)
      throws IOException {
    ActiveMQMessage amqMsg = null;
    byte coreType = coreMessage.getType();
    switch (coreType) {
      case org.apache.activemq.artemis.api.core.Message.BYTES_TYPE:
        amqMsg = new ActiveMQBytesMessage();
        break;
      case org.apache.activemq.artemis.api.core.Message.MAP_TYPE:
        amqMsg = new ActiveMQMapMessage();
        break;
      case org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE:
        amqMsg = new ActiveMQObjectMessage();
        break;
      case org.apache.activemq.artemis.api.core.Message.STREAM_TYPE:
        amqMsg = new ActiveMQStreamMessage();
        break;
      case org.apache.activemq.artemis.api.core.Message.TEXT_TYPE:
        amqMsg = new ActiveMQTextMessage();
        break;
      case org.apache.activemq.artemis.api.core.Message.DEFAULT_TYPE:
        amqMsg = new ActiveMQMessage();
        break;
      default:
        throw new IllegalStateException("Unknown message type: " + coreMessage.getType());
    }

    String type = coreMessage.getStringProperty(new SimpleString("JMSType"));
    if (type != null) {
      amqMsg.setJMSType(type);
    }
    amqMsg.setPersistent(coreMessage.isDurable());
    amqMsg.setExpiration(coreMessage.getExpiration());
    amqMsg.setPriority(coreMessage.getPriority());
    amqMsg.setTimestamp(coreMessage.getTimestamp());

    Long brokerInTime = (Long) coreMessage.getObjectProperty(AMQ_MSG_BROKER_IN_TIME);
    if (brokerInTime == null) {
      brokerInTime = 0L;
    }
    amqMsg.setBrokerInTime(brokerInTime);

    ActiveMQBuffer buffer = coreMessage.getBodyBufferDuplicate();
    Boolean compressProp = (Boolean) coreMessage.getObjectProperty(AMQ_MSG_COMPRESSED);
    boolean isCompressed = compressProp == null ? false : compressProp.booleanValue();
    amqMsg.setCompressed(isCompressed);

    if (buffer != null) {
      buffer.resetReaderIndex();
      byte[] bytes = null;
      synchronized (buffer) {
        if (coreType == org.apache.activemq.artemis.api.core.Message.TEXT_TYPE) {
          SimpleString text = buffer.readNullableSimpleString();
          if (text != null) {
            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(text.length() + 4);
            OutputStream out = bytesOut;
            if (isCompressed) {
              out = new DeflaterOutputStream(out);
            }
            try (DataOutputStream dataOut = new DataOutputStream(out)) {
              MarshallingSupport.writeUTF8(dataOut, text.toString());
              bytes = bytesOut.toByteArray();
            }
          }
        } else if (coreType == org.apache.activemq.artemis.api.core.Message.MAP_TYPE) {
          TypedProperties mapData = new TypedProperties();
          // it could be a null map
          if (buffer.readableBytes() > 0) {
            mapData.decode(buffer);
            Map<String, Object> map = mapData.getMap();
            ByteArrayOutputStream out = new ByteArrayOutputStream(mapData.getEncodeSize());
            OutputStream os = out;
            if (isCompressed) {
              os = new DeflaterOutputStream(os);
            }
            try (DataOutputStream dataOut = new DataOutputStream(os)) {
              MarshallingSupport.marshalPrimitiveMap(map, dataOut);
            }
            bytes = out.toByteArray();
          }

        } else if (coreType == org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE) {
          int len = buffer.readInt();
          bytes = new byte[len];
          buffer.readBytes(bytes);
          if (isCompressed) {
            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
            try (DeflaterOutputStream out = new DeflaterOutputStream(bytesOut)) {
              out.write(bytes);
            }
            bytes = bytesOut.toByteArray();
          }
        } else if (coreType == org.apache.activemq.artemis.api.core.Message.STREAM_TYPE) {
          org.apache.activemq.util.ByteArrayOutputStream bytesOut =
              new org.apache.activemq.util.ByteArrayOutputStream();
          OutputStream out = bytesOut;
          if (isCompressed) {
            out = new DeflaterOutputStream(bytesOut);
          }
          try (DataOutputStream dataOut = new DataOutputStream(out)) {

            boolean stop = false;
            while (!stop && buffer.readable()) {
              byte primitiveType = buffer.readByte();
              switch (primitiveType) {
                case DataConstants.BOOLEAN:
                  MarshallingSupport.marshalBoolean(dataOut, buffer.readBoolean());
                  break;
                case DataConstants.BYTE:
                  MarshallingSupport.marshalByte(dataOut, buffer.readByte());
                  break;
                case DataConstants.BYTES:
                  int len = buffer.readInt();
                  byte[] bytesData = new byte[len];
                  buffer.readBytes(bytesData);
                  MarshallingSupport.marshalByteArray(dataOut, bytesData);
                  break;
                case DataConstants.CHAR:
                  char ch = (char) buffer.readShort();
                  MarshallingSupport.marshalChar(dataOut, ch);
                  break;
                case DataConstants.DOUBLE:
                  double doubleVal = Double.longBitsToDouble(buffer.readLong());
                  MarshallingSupport.marshalDouble(dataOut, doubleVal);
                  break;
                case DataConstants.FLOAT:
                  Float floatVal = Float.intBitsToFloat(buffer.readInt());
                  MarshallingSupport.marshalFloat(dataOut, floatVal);
                  break;
                case DataConstants.INT:
                  MarshallingSupport.marshalInt(dataOut, buffer.readInt());
                  break;
                case DataConstants.LONG:
                  MarshallingSupport.marshalLong(dataOut, buffer.readLong());
                  break;
                case DataConstants.SHORT:
                  MarshallingSupport.marshalShort(dataOut, buffer.readShort());
                  break;
                case DataConstants.STRING:
                  String string = buffer.readNullableString();
                  if (string == null) {
                    MarshallingSupport.marshalNull(dataOut);
                  } else {
                    MarshallingSupport.marshalString(dataOut, string);
                  }
                  break;
                default:
                  // now we stop
                  stop = true;
                  break;
              }
            }
          }
          bytes = bytesOut.toByteArray();
        } else if (coreType == org.apache.activemq.artemis.api.core.Message.BYTES_TYPE) {
          int n = buffer.readableBytes();
          bytes = new byte[n];
          buffer.readBytes(bytes);
          if (isCompressed) {
            int length = bytes.length;
            Deflater deflater = new Deflater();
            try (org.apache.activemq.util.ByteArrayOutputStream compressed =
                new org.apache.activemq.util.ByteArrayOutputStream()) {
              compressed.write(new byte[4]);
              deflater.setInput(bytes);
              deflater.finish();
              byte[] bytesBuf = new byte[1024];
              while (!deflater.finished()) {
                int count = deflater.deflate(bytesBuf);
                compressed.write(bytesBuf, 0, count);
              }
              ByteSequence byteSeq = compressed.toByteSequence();
              ByteSequenceData.writeIntBig(byteSeq, length);
              bytes = Arrays.copyOfRange(byteSeq.data, 0, byteSeq.length);
            } finally {
              deflater.end();
            }
          }
        } else {
          int n = buffer.readableBytes();
          bytes = new byte[n];
          buffer.readBytes(bytes);
          if (isCompressed) {
            try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
                DeflaterOutputStream out = new DeflaterOutputStream(bytesOut)) {
              out.write(bytes);
              bytes = bytesOut.toByteArray();
            }
          }
        }

        buffer.resetReaderIndex(); // this is important for topics as the buffer
        // may be read multiple times
      }

      if (bytes != null) {
        ByteSequence content = new ByteSequence(bytes);
        amqMsg.setContent(content);
      }
    }

    // we need check null because messages may come from other clients
    // and those amq specific attribute may not be set.
    Long arrival = (Long) coreMessage.getObjectProperty(AMQ_MSG_ARRIVAL);
    if (arrival == null) {
      // messages from other sources (like core client) may not set this prop
      arrival = 0L;
    }
    amqMsg.setArrival(arrival);

    String brokerPath = (String) coreMessage.getObjectProperty(AMQ_MSG_BROKER_PATH);
    if (brokerPath != null && brokerPath.isEmpty()) {
      String[] brokers = brokerPath.split(",");
      BrokerId[] bids = new BrokerId[brokers.length];
      for (int i = 0; i < bids.length; i++) {
        bids[i] = new BrokerId(brokers[i]);
      }
      amqMsg.setBrokerPath(bids);
    }

    String clusterPath = (String) coreMessage.getObjectProperty(AMQ_MSG_CLUSTER);
    if (clusterPath != null && clusterPath.isEmpty()) {
      String[] cluster = clusterPath.split(",");
      BrokerId[] bids = new BrokerId[cluster.length];
      for (int i = 0; i < bids.length; i++) {
        bids[i] = new BrokerId(cluster[i]);
      }
      amqMsg.setCluster(bids);
    }

    Integer commandId = (Integer) coreMessage.getObjectProperty(AMQ_MSG_COMMAND_ID);
    if (commandId == null) {
      commandId = -1;
    }
    amqMsg.setCommandId(commandId);

    SimpleString corrId = (SimpleString) coreMessage.getObjectProperty("JMSCorrelationID");
    if (corrId != null) {
      amqMsg.setCorrelationId(corrId.toString());
    }

    byte[] dsBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_DATASTRUCTURE);
    if (dsBytes != null) {
      ByteSequence seq = new ByteSequence(dsBytes);
      DataStructure ds = (DataStructure) marshaller.unmarshal(seq);
      amqMsg.setDataStructure(ds);
    }

    amqMsg.setDestination(OpenWireUtil.toAMQAddress(coreMessage, actualDestination));

    Object value = coreMessage.getObjectProperty(AMQ_MSG_GROUP_ID);
    if (value != null) {
      String groupId = value.toString();
      amqMsg.setGroupID(groupId);
    }

    Integer groupSequence = (Integer) coreMessage.getObjectProperty(AMQ_MSG_GROUP_SEQUENCE);
    if (groupSequence == null) {
      groupSequence = -1;
    }
    amqMsg.setGroupSequence(groupSequence);

    MessageId mid = null;
    byte[] midBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_MESSAGE_ID);
    if (midBytes != null) {
      ByteSequence midSeq = new ByteSequence(midBytes);
      mid = (MessageId) marshaller.unmarshal(midSeq);
    } else {
      mid = new MessageId(UUIDGenerator.getInstance().generateStringUUID() + ":-1");
    }

    amqMsg.setMessageId(mid);

    byte[] origDestBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_ORIG_DESTINATION);
    if (origDestBytes != null) {
      ActiveMQDestination origDest =
          (ActiveMQDestination) marshaller.unmarshal(new ByteSequence(origDestBytes));
      amqMsg.setOriginalDestination(origDest);
    }

    byte[] origTxIdBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_ORIG_TXID);
    if (origTxIdBytes != null) {
      TransactionId origTxId =
          (TransactionId) marshaller.unmarshal(new ByteSequence(origTxIdBytes));
      amqMsg.setOriginalTransactionId(origTxId);
    }

    byte[] producerIdBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_PRODUCER_ID);
    if (producerIdBytes != null) {
      ProducerId producerId = (ProducerId) marshaller.unmarshal(new ByteSequence(producerIdBytes));
      amqMsg.setProducerId(producerId);
    }

    byte[] marshalledBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_MARSHALL_PROP);
    if (marshalledBytes != null) {
      amqMsg.setMarshalledProperties(new ByteSequence(marshalledBytes));
    }

    amqMsg.setRedeliveryCounter(reference.getDeliveryCount() - 1);

    byte[] replyToBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_REPLY_TO);
    if (replyToBytes != null) {
      ActiveMQDestination replyTo =
          (ActiveMQDestination) marshaller.unmarshal(new ByteSequence(replyToBytes));
      amqMsg.setReplyTo(replyTo);
    }

    String userId = (String) coreMessage.getObjectProperty(AMQ_MSG_USER_ID);
    if (userId != null) {
      amqMsg.setUserID(userId);
    }

    Boolean isDroppable = (Boolean) coreMessage.getObjectProperty(AMQ_MSG_DROPPABLE);
    if (isDroppable != null) {
      amqMsg.setDroppable(isDroppable);
    }

    SimpleString dlqCause =
        (SimpleString) coreMessage.getObjectProperty(AMQ_MSG_DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    if (dlqCause != null) {
      try {
        amqMsg.setStringProperty(
            ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY, dlqCause.toString());
      } catch (JMSException e) {
        throw new IOException("failure to set dlq property " + dlqCause, e);
      }
    }

    Set<SimpleString> props = coreMessage.getPropertyNames();
    if (props != null) {
      for (SimpleString s : props) {
        String keyStr = s.toString();
        if (keyStr.startsWith("_AMQ") || keyStr.startsWith("__HDR_")) {
          continue;
        }
        Object prop = coreMessage.getObjectProperty(s);
        try {
          if (prop instanceof SimpleString) {
            amqMsg.setObjectProperty(s.toString(), prop.toString());
          } else {
            amqMsg.setObjectProperty(s.toString(), prop);
          }
        } catch (JMSException e) {
          throw new IOException("exception setting property " + s + " : " + prop, e);
        }
      }
    }
    try {
      amqMsg.onSend();
      amqMsg.setCompressed(isCompressed);
    } catch (JMSException e) {
      throw new IOException("Failed to covert to Openwire message", e);
    }
    return amqMsg;
  }
コード例 #22
0
  @Override
  public ChannelBuffer encode(CompressedChunkMessage message) throws IOException {
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();

    buffer.writeInt(message.getX());
    buffer.writeInt(message.getZ());
    buffer.writeByte(message.isContiguous() ? 1 : 0);
    short sectionsSentBitmap = 0;
    short additionalDataBitMap = 0;

    byte[][] data = message.getData();

    int uncompressedSize = 0;
    for (int i = 0; i < MAX_SECTIONS; ++i) {
      if (data[i] != null) { // This chunk exists! Let's initialize the data for it.
        sectionsSentBitmap |= 1 << i;
        if (message.hasAdditionalData()[i]) {
          additionalDataBitMap |= 1 << i;
        }
        uncompressedSize += data[i].length;
      }
    }

    if (message.isContiguous()) {
      uncompressedSize += message.getBiomeData().length;
    }

    buffer.writeShort(sectionsSentBitmap);
    buffer.writeShort(additionalDataBitMap);
    byte[] uncompressedData = new byte[uncompressedSize];
    int index = 0;
    for (byte[] sectionData : data) {
      if (sectionData != null) {
        System.arraycopy(sectionData, 0, uncompressedData, index, sectionData.length);
        index += sectionData.length;
      }
    }

    if (message.isContiguous()) {
      System.arraycopy(
          message.getBiomeData(), 0, uncompressedData, index, message.getBiomeData().length);
      index += message.getBiomeData().length;
    }

    byte[] compressedData = new byte[uncompressedSize];

    Deflater deflater = new Deflater(COMPRESSION_LEVEL);
    deflater.setInput(uncompressedData);
    deflater.finish();

    int compressed = deflater.deflate(compressedData);
    try {
      if (compressed == 0) {
        throw new IOException("Not all bytes compressed.");
      }
    } finally {
      deflater.end();
    }

    buffer.writeInt(compressed);
    buffer.writeInt(message.getUnused());
    buffer.writeBytes(compressedData, 0, compressed);

    return buffer;
  }
コード例 #23
0
  public void build_bricks() {

    ImagePlus imp;
    ImagePlus orgimp;
    ImageStack stack;
    FileInfo finfo;

    if (lvImgTitle.isEmpty()) return;
    orgimp = WindowManager.getImage(lvImgTitle.get(0));
    imp = orgimp;

    finfo = imp.getFileInfo();
    if (finfo == null) return;

    int[] dims = imp.getDimensions();
    int imageW = dims[0];
    int imageH = dims[1];
    int nCh = dims[2];
    int imageD = dims[3];
    int nFrame = dims[4];
    int bdepth = imp.getBitDepth();
    double xspc = finfo.pixelWidth;
    double yspc = finfo.pixelHeight;
    double zspc = finfo.pixelDepth;
    double z_aspect = Math.max(xspc, yspc) / zspc;

    int orgW = imageW;
    int orgH = imageH;
    int orgD = imageD;
    double orgxspc = xspc;
    double orgyspc = yspc;
    double orgzspc = zspc;

    lv = lvImgTitle.size();
    if (filetype == "JPEG") {
      for (int l = 0; l < lv; l++) {
        if (WindowManager.getImage(lvImgTitle.get(l)).getBitDepth() != 8) {
          IJ.error("A SOURCE IMAGE MUST BE 8BIT GLAYSCALE");
          return;
        }
      }
    }

    // calculate levels
    /*		int baseXY = 256;
    		int baseZ = 256;

    		if (z_aspect < 0.5) baseZ = 128;
    		if (z_aspect > 2.0) baseXY = 128;
    		if (z_aspect >= 0.5 && z_aspect < 1.0) baseZ = (int)(baseZ*z_aspect);
    		if (z_aspect > 1.0 && z_aspect <= 2.0) baseXY = (int)(baseXY/z_aspect);

    		IJ.log("Z_aspect: " + z_aspect);
    		IJ.log("BaseXY: " + baseXY);
    		IJ.log("BaseZ: " + baseZ);
    */

    int baseXY = 256;
    int baseZ = 128;
    int dbXY = Math.max(orgW, orgH) / baseXY;
    if (Math.max(orgW, orgH) % baseXY > 0) dbXY *= 2;
    int dbZ = orgD / baseZ;
    if (orgD % baseZ > 0) dbZ *= 2;
    lv = Math.max(log2(dbXY), log2(dbZ)) + 1;

    int ww = orgW;
    int hh = orgH;
    int dd = orgD;
    for (int l = 0; l < lv; l++) {
      int bwnum = ww / baseXY;
      if (ww % baseXY > 0) bwnum++;
      int bhnum = hh / baseXY;
      if (hh % baseXY > 0) bhnum++;
      int bdnum = dd / baseZ;
      if (dd % baseZ > 0) bdnum++;

      if (bwnum % 2 == 0) bwnum++;
      if (bhnum % 2 == 0) bhnum++;
      if (bdnum % 2 == 0) bdnum++;

      int bw = (bwnum <= 1) ? ww : ww / bwnum + 1 + (ww % bwnum > 0 ? 1 : 0);
      int bh = (bhnum <= 1) ? hh : hh / bhnum + 1 + (hh % bhnum > 0 ? 1 : 0);
      int bd = (bdnum <= 1) ? dd : dd / bdnum + 1 + (dd % bdnum > 0 ? 1 : 0);

      bwlist.add(bw);
      bhlist.add(bh);
      bdlist.add(bd);

      IJ.log("LEVEL: " + l);
      IJ.log("  width: " + ww);
      IJ.log("  hight: " + hh);
      IJ.log("  depth: " + dd);
      IJ.log("  bw: " + bw);
      IJ.log("  bh: " + bh);
      IJ.log("  bd: " + bd);

      int xyl2 = Math.max(ww, hh) / baseXY;
      if (Math.max(ww, hh) % baseXY > 0) xyl2 *= 2;
      if (lv - 1 - log2(xyl2) <= l) {
        ww /= 2;
        hh /= 2;
      }
      IJ.log("  xyl2: " + (lv - 1 - log2(xyl2)));

      int zl2 = dd / baseZ;
      if (dd % baseZ > 0) zl2 *= 2;
      if (lv - 1 - log2(zl2) <= l) dd /= 2;
      IJ.log("  zl2: " + (lv - 1 - log2(zl2)));

      if (l < lv - 1) {
        lvImgTitle.add(lvImgTitle.get(0) + "_level" + (l + 1));
        IJ.selectWindow(lvImgTitle.get(0));
        IJ.run(
            "Scale...",
            "x=- y=- z=- width="
                + ww
                + " height="
                + hh
                + " depth="
                + dd
                + " interpolation=Bicubic average process create title="
                + lvImgTitle.get(l + 1));
      }
    }

    for (int l = 0; l < lv; l++) {
      IJ.log(lvImgTitle.get(l));
    }

    Document doc = newXMLDocument();
    Element root = doc.createElement("BRK");
    root.setAttribute("version", "1.0");
    root.setAttribute("nLevel", String.valueOf(lv));
    root.setAttribute("nChannel", String.valueOf(nCh));
    root.setAttribute("nFrame", String.valueOf(nFrame));
    doc.appendChild(root);

    for (int l = 0; l < lv; l++) {
      IJ.showProgress(0.0);

      int[] dims2 = imp.getDimensions();
      IJ.log(
          "W: "
              + String.valueOf(dims2[0])
              + " H: "
              + String.valueOf(dims2[1])
              + " C: "
              + String.valueOf(dims2[2])
              + " D: "
              + String.valueOf(dims2[3])
              + " T: "
              + String.valueOf(dims2[4])
              + " b: "
              + String.valueOf(bdepth));

      bw = bwlist.get(l).intValue();
      bh = bhlist.get(l).intValue();
      bd = bdlist.get(l).intValue();

      boolean force_pow2 = false;
      /*			if(IsPowerOf2(bw) && IsPowerOf2(bh) && IsPowerOf2(bd)) force_pow2 = true;

      			if(force_pow2){
      				//force pow2
      				if(Pow2(bw) > bw) bw = Pow2(bw)/2;
      				if(Pow2(bh) > bh) bh = Pow2(bh)/2;
      				if(Pow2(bd) > bd) bd = Pow2(bd)/2;
      			}

      			if(bw > imageW) bw = (Pow2(imageW) == imageW) ? imageW : Pow2(imageW)/2;
      			if(bh > imageH) bh = (Pow2(imageH) == imageH) ? imageH : Pow2(imageH)/2;
      			if(bd > imageD) bd = (Pow2(imageD) == imageD) ? imageD : Pow2(imageD)/2;

      */
      if (bw > imageW) bw = imageW;
      if (bh > imageH) bh = imageH;
      if (bd > imageD) bd = imageD;

      if (bw <= 1 || bh <= 1 || bd <= 1) break;

      if (filetype == "JPEG" && (bw < 8 || bh < 8)) break;

      Element lvnode = doc.createElement("Level");
      lvnode.setAttribute("lv", String.valueOf(l));
      lvnode.setAttribute("imageW", String.valueOf(imageW));
      lvnode.setAttribute("imageH", String.valueOf(imageH));
      lvnode.setAttribute("imageD", String.valueOf(imageD));
      lvnode.setAttribute("xspc", String.valueOf(xspc));
      lvnode.setAttribute("yspc", String.valueOf(yspc));
      lvnode.setAttribute("zspc", String.valueOf(zspc));
      lvnode.setAttribute("bitDepth", String.valueOf(bdepth));
      root.appendChild(lvnode);

      Element brksnode = doc.createElement("Bricks");
      brksnode.setAttribute("brick_baseW", String.valueOf(bw));
      brksnode.setAttribute("brick_baseH", String.valueOf(bh));
      brksnode.setAttribute("brick_baseD", String.valueOf(bd));
      lvnode.appendChild(brksnode);

      ArrayList<Brick> bricks = new ArrayList<Brick>();
      int mw, mh, md, mw2, mh2, md2;
      double tx0, ty0, tz0, tx1, ty1, tz1;
      double bx0, by0, bz0, bx1, by1, bz1;
      for (int k = 0; k < imageD; k += bd) {
        if (k > 0) k--;
        for (int j = 0; j < imageH; j += bh) {
          if (j > 0) j--;
          for (int i = 0; i < imageW; i += bw) {
            if (i > 0) i--;
            mw = Math.min(bw, imageW - i);
            mh = Math.min(bh, imageH - j);
            md = Math.min(bd, imageD - k);

            if (force_pow2) {
              mw2 = Pow2(mw);
              mh2 = Pow2(mh);
              md2 = Pow2(md);
            } else {
              mw2 = mw;
              mh2 = mh;
              md2 = md;
            }

            if (filetype == "JPEG") {
              if (mw2 < 8) mw2 = 8;
              if (mh2 < 8) mh2 = 8;
            }

            tx0 = i == 0 ? 0.0d : ((mw2 - mw + 0.5d) / mw2);
            ty0 = j == 0 ? 0.0d : ((mh2 - mh + 0.5d) / mh2);
            tz0 = k == 0 ? 0.0d : ((md2 - md + 0.5d) / md2);

            tx1 = 1.0d - 0.5d / mw2;
            if (mw < bw) tx1 = 1.0d;
            if (imageW - i == bw) tx1 = 1.0d;

            ty1 = 1.0d - 0.5d / mh2;
            if (mh < bh) ty1 = 1.0d;
            if (imageH - j == bh) ty1 = 1.0d;

            tz1 = 1.0d - 0.5d / md2;
            if (md < bd) tz1 = 1.0d;
            if (imageD - k == bd) tz1 = 1.0d;

            bx0 = i == 0 ? 0.0d : (i + 0.5d) / (double) imageW;
            by0 = j == 0 ? 0.0d : (j + 0.5d) / (double) imageH;
            bz0 = k == 0 ? 0.0d : (k + 0.5d) / (double) imageD;

            bx1 = Math.min((i + bw - 0.5d) / (double) imageW, 1.0d);
            if (imageW - i == bw) bx1 = 1.0d;

            by1 = Math.min((j + bh - 0.5d) / (double) imageH, 1.0d);
            if (imageH - j == bh) by1 = 1.0d;

            bz1 = Math.min((k + bd - 0.5d) / (double) imageD, 1.0d);
            if (imageD - k == bd) bz1 = 1.0d;

            int x, y, z;
            x = i - (mw2 - mw);
            y = j - (mh2 - mh);
            z = k - (md2 - md);
            bricks.add(
                new Brick(
                    x, y, z, mw2, mh2, md2, 0, 0, tx0, ty0, tz0, tx1, ty1, tz1, bx0, by0, bz0, bx1,
                    by1, bz1));
          }
        }
      }

      Element fsnode = doc.createElement("Files");
      lvnode.appendChild(fsnode);

      stack = imp.getStack();

      int totalbricknum = nFrame * nCh * bricks.size();
      int curbricknum = 0;
      for (int f = 0; f < nFrame; f++) {
        for (int ch = 0; ch < nCh; ch++) {
          int sizelimit = bdsizelimit * 1024 * 1024;
          int bytecount = 0;
          int filecount = 0;
          int pd_bufsize = Math.max(sizelimit, bw * bh * bd * bdepth / 8);
          byte[] packed_data = new byte[pd_bufsize];
          String base_dataname =
              basename
                  + "_Lv"
                  + String.valueOf(l)
                  + "_Ch"
                  + String.valueOf(ch)
                  + "_Fr"
                  + String.valueOf(f);
          String current_dataname = base_dataname + "_data" + filecount;

          Brick b_first = bricks.get(0);
          if (b_first.z_ != 0) IJ.log("warning");
          int st_z = b_first.z_;
          int ed_z = b_first.z_ + b_first.d_;
          LinkedList<ImageProcessor> iplist = new LinkedList<ImageProcessor>();
          for (int s = st_z; s < ed_z; s++)
            iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));

          //					ImagePlus test;
          //					ImageStack tsst;
          //					test = NewImage.createByteImage("test", imageW, imageH, imageD,
          // NewImage.FILL_BLACK);
          //					tsst = test.getStack();
          for (int i = 0; i < bricks.size(); i++) {
            Brick b = bricks.get(i);

            if (ed_z > b.z_ || st_z < b.z_ + b.d_) {
              if (b.z_ > st_z) {
                for (int s = 0; s < b.z_ - st_z; s++) iplist.pollFirst();
                st_z = b.z_;
              } else if (b.z_ < st_z) {
                IJ.log("warning");
                for (int s = st_z - 1; s > b.z_; s--)
                  iplist.addFirst(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));
                st_z = b.z_;
              }

              if (b.z_ + b.d_ > ed_z) {
                for (int s = ed_z; s < b.z_ + b.d_; s++)
                  iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));
                ed_z = b.z_ + b.d_;
              } else if (b.z_ + b.d_ < ed_z) {
                IJ.log("warning");
                for (int s = 0; s < ed_z - (b.z_ + b.d_); s++) iplist.pollLast();
                ed_z = b.z_ + b.d_;
              }
            } else {
              IJ.log("warning");
              iplist.clear();
              st_z = b.z_;
              ed_z = b.z_ + b.d_;
              for (int s = st_z; s < ed_z; s++)
                iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));
            }

            if (iplist.size() != b.d_) {
              IJ.log("Stack Error");
              return;
            }

            //						int zz = st_z;

            int bsize = 0;
            byte[] bdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8];
            Iterator<ImageProcessor> ipite = iplist.iterator();
            while (ipite.hasNext()) {

              //							ImageProcessor tsip = tsst.getProcessor(zz+1);

              ImageProcessor ip = ipite.next();
              ip.setRoi(b.x_, b.y_, b.w_, b.h_);
              if (bdepth == 8) {
                byte[] data = (byte[]) ip.crop().getPixels();
                System.arraycopy(data, 0, bdata, bsize, data.length);
                bsize += data.length;
              } else if (bdepth == 16) {
                ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8);
                buffer.order(ByteOrder.LITTLE_ENDIAN);
                short[] data = (short[]) ip.crop().getPixels();
                for (short e : data) buffer.putShort(e);
                System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length);
                bsize += buffer.array().length;
              } else if (bdepth == 32) {
                ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8);
                buffer.order(ByteOrder.LITTLE_ENDIAN);
                float[] data = (float[]) ip.crop().getPixels();
                for (float e : data) buffer.putFloat(e);
                System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length);
                bsize += buffer.array().length;
              }
            }

            String filename =
                basename
                    + "_Lv"
                    + String.valueOf(l)
                    + "_Ch"
                    + String.valueOf(ch)
                    + "_Fr"
                    + String.valueOf(f)
                    + "_ID"
                    + String.valueOf(i);

            int offset = bytecount;
            int datasize = bdata.length;

            if (filetype == "RAW") {
              int dummy = -1;
              // do nothing
            }
            if (filetype == "JPEG" && bdepth == 8) {
              try {
                DataBufferByte db = new DataBufferByte(bdata, datasize);
                Raster raster = Raster.createPackedRaster(db, b.w_, b.h_ * b.d_, 8, null);
                BufferedImage img =
                    new BufferedImage(b.w_, b.h_ * b.d_, BufferedImage.TYPE_BYTE_GRAY);
                img.setData(raster);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
                String format = "jpg";
                Iterator<javax.imageio.ImageWriter> iter =
                    ImageIO.getImageWritersByFormatName("jpeg");
                javax.imageio.ImageWriter writer = iter.next();
                ImageWriteParam iwp = writer.getDefaultWriteParam();
                iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                iwp.setCompressionQuality((float) jpeg_quality * 0.01f);
                writer.setOutput(ios);
                writer.write(null, new IIOImage(img, null, null), iwp);
                // ImageIO.write(img, format, baos);
                bdata = baos.toByteArray();
                datasize = bdata.length;
              } catch (IOException e) {
                e.printStackTrace();
                return;
              }
            }
            if (filetype == "ZLIB") {
              byte[] tmpdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8];
              Deflater compresser = new Deflater();
              compresser.setInput(bdata);
              compresser.setLevel(Deflater.DEFAULT_COMPRESSION);
              compresser.setStrategy(Deflater.DEFAULT_STRATEGY);
              compresser.finish();
              datasize = compresser.deflate(tmpdata);
              bdata = tmpdata;
              compresser.end();
            }

            if (bytecount + datasize > sizelimit && bytecount > 0) {
              BufferedOutputStream fis = null;
              try {
                File file = new File(directory + current_dataname);
                fis = new BufferedOutputStream(new FileOutputStream(file));
                fis.write(packed_data, 0, bytecount);
              } catch (IOException e) {
                e.printStackTrace();
                return;
              } finally {
                try {
                  if (fis != null) fis.close();
                } catch (IOException e) {
                  e.printStackTrace();
                  return;
                }
              }
              filecount++;
              current_dataname = base_dataname + "_data" + filecount;
              bytecount = 0;
              offset = 0;
              System.arraycopy(bdata, 0, packed_data, bytecount, datasize);
              bytecount += datasize;
            } else {
              System.arraycopy(bdata, 0, packed_data, bytecount, datasize);
              bytecount += datasize;
            }

            Element filenode = doc.createElement("File");
            filenode.setAttribute("filename", current_dataname);
            filenode.setAttribute("channel", String.valueOf(ch));
            filenode.setAttribute("frame", String.valueOf(f));
            filenode.setAttribute("brickID", String.valueOf(i));
            filenode.setAttribute("offset", String.valueOf(offset));
            filenode.setAttribute("datasize", String.valueOf(datasize));
            filenode.setAttribute("filetype", String.valueOf(filetype));

            fsnode.appendChild(filenode);

            curbricknum++;
            IJ.showProgress((double) (curbricknum) / (double) (totalbricknum));
          }
          if (bytecount > 0) {
            BufferedOutputStream fis = null;
            try {
              File file = new File(directory + current_dataname);
              fis = new BufferedOutputStream(new FileOutputStream(file));
              fis.write(packed_data, 0, bytecount);
            } catch (IOException e) {
              e.printStackTrace();
              return;
            } finally {
              try {
                if (fis != null) fis.close();
              } catch (IOException e) {
                e.printStackTrace();
                return;
              }
            }
          }
        }
      }

      for (int i = 0; i < bricks.size(); i++) {
        Brick b = bricks.get(i);
        Element bricknode = doc.createElement("Brick");
        bricknode.setAttribute("id", String.valueOf(i));
        bricknode.setAttribute("st_x", String.valueOf(b.x_));
        bricknode.setAttribute("st_y", String.valueOf(b.y_));
        bricknode.setAttribute("st_z", String.valueOf(b.z_));
        bricknode.setAttribute("width", String.valueOf(b.w_));
        bricknode.setAttribute("height", String.valueOf(b.h_));
        bricknode.setAttribute("depth", String.valueOf(b.d_));
        brksnode.appendChild(bricknode);

        Element tboxnode = doc.createElement("tbox");
        tboxnode.setAttribute("x0", String.valueOf(b.tx0_));
        tboxnode.setAttribute("y0", String.valueOf(b.ty0_));
        tboxnode.setAttribute("z0", String.valueOf(b.tz0_));
        tboxnode.setAttribute("x1", String.valueOf(b.tx1_));
        tboxnode.setAttribute("y1", String.valueOf(b.ty1_));
        tboxnode.setAttribute("z1", String.valueOf(b.tz1_));
        bricknode.appendChild(tboxnode);

        Element bboxnode = doc.createElement("bbox");
        bboxnode.setAttribute("x0", String.valueOf(b.bx0_));
        bboxnode.setAttribute("y0", String.valueOf(b.by0_));
        bboxnode.setAttribute("z0", String.valueOf(b.bz0_));
        bboxnode.setAttribute("x1", String.valueOf(b.bx1_));
        bboxnode.setAttribute("y1", String.valueOf(b.by1_));
        bboxnode.setAttribute("z1", String.valueOf(b.bz1_));
        bricknode.appendChild(bboxnode);
      }

      if (l < lv - 1) {
        imp = WindowManager.getImage(lvImgTitle.get(l + 1));
        int[] newdims = imp.getDimensions();
        imageW = newdims[0];
        imageH = newdims[1];
        imageD = newdims[3];
        xspc = orgxspc * ((double) orgW / (double) imageW);
        yspc = orgyspc * ((double) orgH / (double) imageH);
        zspc = orgzspc * ((double) orgD / (double) imageD);
        bdepth = imp.getBitDepth();
      }
    }

    File newXMLfile = new File(directory + basename + ".vvd");
    writeXML(newXMLfile, doc);

    for (int l = 1; l < lv; l++) {
      imp = WindowManager.getImage(lvImgTitle.get(l));
      imp.changes = false;
      imp.close();
    }
  }
コード例 #24
0
ファイル: IOUtils.java プロジェクト: KenaDess/limewire
 public static void close(Deflater deflater) {
   if (deflater != null) {
     deflater.end();
   }
 }
コード例 #25
0
 @Override
 public void end() {
   compressor.end();
 }