Package java.util.zip

Examples of java.util.zip.DeflaterOutputStream


  @Override
  public void writeObject(marauroa.common.net.OutputSerializer out) throws IOException {
    super.writeObject(out);

    ByteArrayOutputStream array = new ByteArrayOutputStream();
    DeflaterOutputStream out_stream = new DeflaterOutputStream(array);
    OutputSerializer serializer = new OutputSerializer(out_stream);
    serializer.setProtocolVersion(out.getProtocolVersion());

    serializer.write(contents);
    int size = RPClass.size();

    // sort out the default rp class if it is there
    for (Iterator<RPClass> it = RPClass.iterator(); it.hasNext();) {
      RPClass rp_class = it.next();
      if ("".equals(rp_class.getName())) {
        size--;
        break;
      }
    }

    serializer.write(size);
    for (Iterator<RPClass> it = RPClass.iterator(); it.hasNext();) {
      RPClass rp_class = it.next();
      if (!"".equals(rp_class.getName())) // sort out default class if it
      // is there
      {
        serializer.write(rp_class);
      }
    }

    out_stream.close();

    out.write(array.toByteArray());
  }
View Full Code Here


  
   protected byte[] deflate(Object object) throws IOException
   {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      Deflater def = new Deflater(java.util.zip.Deflater.BEST_COMPRESSION);
      DeflaterOutputStream dos = new DeflaterOutputStream(baos, def);
     
      ObjectOutputStream out = new ObjectOutputStream(dos);
      out.writeObject(object);
      out.close();
      dos.finish();
      dos.close();
     
      return baos.toByteArray();
   }
View Full Code Here

            }

            // Debug.debug("uncompressed", uncompressed.length);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DeflaterOutputStream dos = new DeflaterOutputStream(baos);
            int chunk_size = 256 * 1024;
            for (int index = 0; index < uncompressed.length; index += chunk_size)
            {
                int end = Math.min(uncompressed.length, index + chunk_size);
                int length = end - index;

                dos.write(uncompressed, index, length);
                dos.flush();
                baos.flush();

                byte compressed[] = baos.toByteArray();
                baos.reset();
                if (compressed.length > 0)
                {
                    // Debug.debug("compressed", compressed.length);
                    writeChunkIDAT(os, compressed);
                }

            }
            {
                dos.finish();
                byte compressed[] = baos.toByteArray();
                if (compressed.length > 0)
                {
                    // Debug.debug("compressed final", compressed.length);
                    writeChunkIDAT(os, compressed);
View Full Code Here

  {
    try
    {
      ByteArrayInputStream inputStream = new ByteArrayInputStream(data, offset, length);
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      DeflaterOutputStream outputFilter = new DeflaterOutputStream(outputStream);
      transform(inputStream, outputFilter);
      return outputStream.toByteArray();
    }
    catch(IOException e)
    {throw new RuntimeException("Encoding failed.",e);}
View Full Code Here

                               int compressionType) throws IOException {

        InputStream          in        = null;
        OutputStream         f         = null;
        OutputStream         fOut      = null;
        DeflaterOutputStream deflater  = null;
        boolean              completed = false;

        // if there is no file
        if (!storage.isStreamElement(infilename)) {
            return;
        }

        try {
            byte[] b = new byte[COPY_BLOCK_SIZE];

            in   = storage.openInputStreamElement(infilename);
            f    = storage.openOutputStreamElement(outfilename);
            fOut = f;

            switch (compressionType) {

                case COMPRESSION_ZIP :
                    f = deflater = new DeflaterOutputStream(f,
                            new Deflater(Deflater.BEST_SPEED), b.length);
                    break;

                case COMPRESSION_GZIP :
                    f = deflater = new GZIPOutputStream(f, b.length);
                    break;

                case COMPRESSION_NONE :
                    break;

                default :
                    throw new RuntimeException("FileArchiver"
                                               + compressionType);
            }

            while (true) {
                int l = in.read(b, 0, b.length);

                if (l == -1) {
                    break;
                }

                f.write(b, 0, l);
            }

            completed = true;
        } catch (Throwable e) {
            throw JavaSystem.toIOException(e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }

                if (f != null) {
                    if (deflater != null) {
                        deflater.finish();
                    }

                    if (fOut instanceof FileOutputStream) {
                        storage.getFileSync(fOut).sync();
                    }
View Full Code Here

                        final boolean changed = isChanged(current, previous, x0, y0, bwidth, bheight, width, height);

                        if (changed) {
                            ByteArrayOutputStream blaos = new ByteArrayOutputStream(4 * 1024);

                            DeflaterOutputStream dos = new DeflaterOutputStream(blaos);

                            for (int y = 0; y < bheight; y++) {
                                dos.write(current, 3 * ((y0 + bheight - y - 1) * width + x0), 3 * bwidth);
                            }

                            dos.finish();

                            final byte[] bbuf = blaos.toByteArray();
                            final int written = bbuf.length;

                            // write DataSize
View Full Code Here

          if (changed)
          {
            ByteArrayOutputStream blaos = new ByteArrayOutputStream(4 * 1024);

            DeflaterOutputStream dos = new DeflaterOutputStream(blaos);

            for (int y = 0; y < bheight; y++)
            {
              dos.write(current, 3 * ((y0 + bheight - y - 1) * width + x0), 3 * bwidth);
            }

            dos.finish();

            final byte[] bbuf = blaos.toByteArray();
            final int written = bbuf.length;

            // write DataSize
View Full Code Here

   * @return the encode byte array
   * @throws IOException
   */
  public byte[] encode() throws IOException {
    final ByteArrayOutputStream array = new ByteArrayOutputStream();
    final DeflaterOutputStream out_stream = new DeflaterOutputStream(array);
    final OutputSerializer out = new OutputSerializer(out_stream);

    writeObject(out);
    out_stream.close();

    return array.toByteArray();
  }
View Full Code Here

   
      _out.writeInt(0);

      _bodyOut = _out.getBytesOutputStream();
   
      _deflateOut = new DeflaterOutputStream(_bodyOut);
    }
View Full Code Here

        bytesPerPixel = (encodeAlpha) ? 4 : 3;

        Deflater scrunch = new Deflater(compressionLevel);
        ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024);

        DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes, scrunch);
        try {
            while (rowsLeft > 0) {
                nRows = Math.min(32767 / (width * (bytesPerPixel + 1)), rowsLeft);
                nRows = Math.max( nRows, 1 );

                int[] pixels = new int[width * nRows];

                pg = new PixelGrabber(image, 0, startRow,
                    width, nRows, pixels, 0, width);
                try {
                    pg.grabPixels();
                }
                catch (Exception e) {
                    System.err.println("interrupted waiting for pixels!");
                    return false;
                }
                if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
                    System.err.println("image fetch aborted or errored");
                    return false;
                }

                /*
                 * Create a data chunk. scanLines adds "nRows" for
                 * the filter bytes.
                 */
                scanLines = new byte[width * nRows * bytesPerPixel +  nRows];

                if (filter == FILTER_SUB) {
                    leftBytes = new byte[16];
                }
                if (filter == FILTER_UP) {
                    priorRow = new byte[width * bytesPerPixel];
                }

                scanPos = 0;
                startPos = 1;
                for (int i = 0; i < width * nRows; i++) {
                    if (i % width == 0) {
                        scanLines[scanPos++] = (byte) filter;
                        startPos = scanPos;
                    }
                    scanLines[scanPos++] = (byte) ((pixels[i] >> 16) & 0xff);
                    scanLines[scanPos++] = (byte) ((pixels[i] >>  8) & 0xff);
                    scanLines[scanPos++] = (byte) ((pixels[i]) & 0xff);
                    if (encodeAlpha) {
                        scanLines[scanPos++] = (byte) ((pixels[i] >> 24) & 0xff);
                    }
                    if ((i % width == width - 1) && (filter != FILTER_NONE)) {
                        if (filter == FILTER_SUB) {
                            filterSub(scanLines, startPos, width);
                        }
                        if (filter == FILTER_UP) {
                            filterUp(scanLines, startPos, width);
                        }
                    }
                }

                /*
                 * Write these lines to the output area
                 */
                compBytes.write(scanLines, 0, scanPos);

                startRow += nRows;
                rowsLeft -= nRows;
            }
            compBytes.close();

            /*
             * Write the compressed bytes
             */
            compressedLines = outBytes.toByteArray();
View Full Code Here

TOP

Related Classes of java.util.zip.DeflaterOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.