Java Source Code: org.xmlBlaster.util.protocol.ZBlockOutputStream


   1: package org.xmlBlaster.util.protocol;
   2: 
   3: import java.io.IOException;
   4: import java.util.zip.Deflater;
   5: 
   6: /**
   7: * @author Patrice Espié
   8: * @author modifications by Balázs Póka
   9: * Licensing: LGPL
  10: */
  11:	  public class ZBlockOutputStream extends FilterOutputStream {
  12:    
  13:    public static final int MAXBUFFERSIZE=20000;
  14:    private int minCompress;
  15:    
  16:    private byte[] buffer;
  17:    private byte[] compBuffer;
  18:    private int writeIndex;
  19:    private Deflater deflater;
  20:
  21:	      public ZBlockOutputStream(OutputStream out, int minCompress) {
  22:        super(out);
  23:        this.minCompress=minCompress;
  24:        buffer=new byte[MAXBUFFERSIZE];
  25:        compBuffer=new byte[MAXBUFFERSIZE];
  26:        writeIndex=0;
  27:        deflater=new Deflater(Deflater.BEST_COMPRESSION);
  28:    }
  29:
  30:	      public synchronized void write(byte[] b) throws IOException {
  31:        write(b, 0, b.length);
  32:    }
  33:
  34:	      public synchronized void write(byte[] b, int off, int len) throws IOException {
  35:        int written=0;
  36:        
  37:	          while(written < len) {
  38:	              if (writeIndex == buffer.length) {
  39:                flush();
  40:            }
  41:
  42:            int toWrite = Math.min(len - written, buffer.length - writeIndex);
  43:            System.arraycopy(b, off + written, buffer, writeIndex, toWrite);
  44:            written += toWrite;
  45:            writeIndex += toWrite;
  46:        }
  47:    }
  48:
  49:	      public synchronized void write(int b) throws IOException {
  50:	          if (writeIndex == buffer.length) {
  51:            flush();
  52:        }
  53:
  54:        buffer[writeIndex++] = (byte)b;
  55:    }
  56:
  57:	      public synchronized void flush() throws IOException {
  58:        int compSize = 0;
  59:        boolean sendCompressed;
  60:        if (writeIndex == 0) return;
  61:
  62:	          if (writeIndex >= minCompress) {
  63:            deflater.reset();
  64:            deflater.setInput(buffer, 0, writeIndex);
  65:            deflater.finish();
  66:
  67:	  /*            if (compBuffer.length < writeIndex * 2 + 40960) {
  68://                System.out.println("OUT1 allocating "+(writeIndex * 2 + 40960));
  69:                compBuffer = new byte[writeIndex * 2 + 40960];
  70:            }
  71:*/
  72:            compSize = deflater.deflate(compBuffer);
  73:
  74:	              if (compSize <= 0) {
  75:                throw new IOException("Compression exception, got 0 bytes output");
  76:            }
  77:
  78:            sendCompressed = compSize < writeIndex;
  79:        } else {
  80:            sendCompressed = false;
  81:        }
  82:
  83:	          if (sendCompressed) {
  84://            System.out.println("Sending compressed "+writeIndex+"->"+compSize+" bytes");
  85:            super.out.write(1);
  86:            super.out.write(writeIndex >> 24 & 0xff);
  87:            super.out.write(writeIndex >> 16 & 0xff);
  88:            super.out.write(writeIndex >> 8 & 0xff);
  89:            super.out.write(writeIndex & 0xff);
  90:            super.out.write(compSize >> 24 & 0xff);
  91:            super.out.write(compSize >> 16 & 0xff);
  92:            super.out.write(compSize >> 8 & 0xff);
  93:            super.out.write(compSize & 0xff);
  94:            super.out.write(compBuffer, 0, compSize);
  95:            super.out.flush();
  96:            writeIndex = 0;
  97:        }
  98:	          else {
  99://            System.out.println("Sending uncompressed "+writeIndex+" bytes");
 100:            super.out.write(0);
 101:            super.out.write(writeIndex >> 24 & 0xff);
 102:            super.out.write(writeIndex >> 16 & 0xff);
 103:            super.out.write(writeIndex >> 8 & 0xff);
 104:            super.out.write(writeIndex & 0xff);
 105:            super.out.write(buffer, 0, writeIndex);
 106:            super.out.flush();
 107:            writeIndex = 0;
 108:        }
 109:    }
 110:
 111:}