Java Source Code: com.waterken.enc.deflate.Encoded


   1: /*
   2:  * Copyright 2002 Waterken Inc. All rights reserved.
   3:  * Use is subject to license terms.
   4:  */
   5: package com.waterken.enc.deflate;
   6: 
   7: import com.waterken.adt.AsBits;
   8: import com.waterken.adt.Inspector;
   9: import com.waterken.adt.Source;
  10: import com.waterken.enc.io.AsOutputStream;
  11: import com.waterken.enc.io.Copying;
  12: 
  13: import java.io.IOException;
  14: import java.lang.reflect.UndeclaredThrowableException;
  15: import java.util.zip.Deflater;
  16: import java.util.zip.DeflaterOutputStream;
  17: 
  18: /**
  19:  * Produces the DEFLATE encoding of binary data.
  20:  * @author  Tyler
  21:  */
  22: public final
  23: class Encoded implements Source, com.waterken.Exportable, Serializable
  24:	  {
  25:    static final long serialVersionUID = 5675432798298694136L;
  26:
  27:    /**
  28:     * @serial  The compression level.
  29:     */
  30:    private int level;
  31:
  32:    /**
  33:     * @serial  The decoded.
  34:     */
  35:    private Source decoded;
  36:
  37:    private
  38:    Encoded()
  39:    {}
  40:
  41:    private
  42:    Encoded(int level, Source decoded)
  43:	      {
  44:        this.level = level;
  45:        this.decoded = decoded;
  46:    }
  47:
  48:    /**
  49:     * Constructs an <code>Encoded</code>.
  50:     * @param   level       The compression level.
  51:     * @param   decoded     The decoded.
  52:     */
  53:    public static
  54:    Source make(int level, Source decoded)
  55:	      {
  56:        if(null == decoded)
  57:            throw new IllegalArgumentException();
  58:        return decoded instanceof Decoded
  59:            ? ((Decoded)decoded).getEncoded()
  60:            : new Encoded(level, decoded);
  61:    }
  62:
  63:    // Object interface.
  64:
  65:    /**
  66:     * Is the given object an equivalent instance of <code>Encoded</code>?
  67:     * @param   o       The compared to object.
  68:     * @return  <code>true</code> if the same, else <code>false</code>.
  69:     */
  70:    public
  71:    boolean equals(Object o)
  72:	      {
  73:        return o instanceof Encoded && level == ((Encoded)o).level && decoded.equals(((Encoded)o).decoded);
  74:    }
  75:
  76:    /**
  77:     * Calculates the hash code.
  78:     */
  79:    public
  80:    int hashCode()
  81:	      {
  82:        return level + decoded.hashCode() + (int)serialVersionUID;
  83:    }
  84:
  85:    /**
  86:     * Creates a decoded description.
  87:     */
  88:    public
  89:    String toString()
  90:	      {
  91:        return getClass().getName() + "( " + level + ", " + decoded + " )";
  92:    }
  93:
  94:    // Source interface.
  95:
  96:    /**
  97:     * Produces the  encoding of binary data.
  98:     * @param   out     The bit inspector.
  99:     * @return  The last <code>out</code>.
 100:     */
 101:    public
 102:    Inspector inspect(Inspector out)
 103:	      {
 104:        Deflater deflater = new Deflater(level);
 105:        try
 106:	          {
 107:            AsOutputStream base = new AsOutputStream(out);
 108:            DeflaterOutputStream s = new DeflaterOutputStream(base, deflater);
 109:            Copying.copyBits(decoded, s);
 110:            s.flush();
 111:            s.close();
 112:            out = AsOutputStream.unwrap(base);
 113:        }
 114:        catch(IOException e)
 115:	          {
 116:            throw new UndeclaredThrowableException(e);
 117:        }
 118:        finally
 119:	          {
 120:            deflater.end();
 121:        }
 122:        return out;
 123:    }
 124:
 125:    // Encoded interface.
 126:
 127:    /**
 128:     * Gets the compression level.
 129:     */
 130:    public
 131:    int getLevel()
 132:	      {
 133:        return level;
 134:    }
 135:
 136:    /**
 137:     * Gets the decoded content.
 138:     */
 139:    public
 140:    Source getDecoded()
 141:	      {
 142:        return decoded;
 143:    }
 144:}