001: package org.bouncycastle.openpgp;
002:
003: import java.io.EOFException;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.util.zip.Inflater;
007: import java.util.zip.InflaterInputStream;
008:
009: import org.bouncycastle.bcpg.BCPGInputStream;
010: import org.bouncycastle.bcpg.CompressedDataPacket;
011: import org.bouncycastle.bcpg.CompressionAlgorithmTags;
012: import org.bouncycastle.apache.bzip2.CBZip2InputStream;
013:
014: /**
015: * Compressed data objects.
016: */
017: public class PGPCompressedData implements CompressionAlgorithmTags {
018: CompressedDataPacket data;
019:
020: public PGPCompressedData(BCPGInputStream pIn) throws IOException {
021: data = (CompressedDataPacket) pIn.readPacket();
022: }
023:
024: /**
025: * Return the algorithm used for compression
026: *
027: * @return algorithm code
028: */
029: public int getAlgorithm() {
030: return data.getAlgorithm();
031: }
032:
033: /**
034: * Return the raw input stream contained in the object.
035: *
036: * @return InputStream
037: */
038: public InputStream getInputStream() {
039: return data.getInputStream();
040: }
041:
042: /**
043: * Return an uncompressed input stream which allows reading of the
044: * compressed data.
045: *
046: * @return InputStream
047: * @throws PGPException
048: */
049: public InputStream getDataStream() throws PGPException {
050: if (this .getAlgorithm() == UNCOMPRESSED) {
051: return this .getInputStream();
052: }
053: if (this .getAlgorithm() == ZIP) {
054: return new InflaterInputStream(this .getInputStream(),
055: new Inflater(true)) {
056: // If the "nowrap" inflater option is used the stream can
057: // apparently overread - we override fill() and provide
058: // an extra byte for the end of the input stream to get
059: // around this.
060: //
061: // Totally weird...
062: //
063: protected void fill() throws IOException {
064: if (eof) {
065: throw new EOFException(
066: "Unexpected end of ZIP input stream");
067: }
068:
069: len = this .in.read(buf, 0, buf.length);
070:
071: if (len == -1) {
072: buf[0] = 0;
073: len = 1;
074: eof = true;
075: }
076:
077: inf.setInput(buf, 0, len);
078: }
079:
080: private boolean eof = false;
081: };
082: }
083: if (this .getAlgorithm() == ZLIB) {
084: return new InflaterInputStream(this .getInputStream()) {
085: // If the "nowrap" inflater option is used the stream can
086: // apparently overread - we override fill() and provide
087: // an extra byte for the end of the input stream to get
088: // around this.
089: //
090: // Totally weird...
091: //
092: protected void fill() throws IOException {
093: if (eof) {
094: throw new EOFException(
095: "Unexpected end of ZIP input stream");
096: }
097:
098: len = this .in.read(buf, 0, buf.length);
099:
100: if (len == -1) {
101: buf[0] = 0;
102: len = 1;
103: eof = true;
104: }
105:
106: inf.setInput(buf, 0, len);
107: }
108:
109: private boolean eof = false;
110: };
111: }
112: if (this .getAlgorithm() == BZIP2) {
113: try {
114: return new CBZip2InputStream(this .getInputStream());
115: } catch (IOException e) {
116: throw new PGPException("I/O problem with stream: " + e,
117: e);
118: }
119: }
120:
121: throw new PGPException(
122: "can't recognise compression algorithm: "
123: + this.getAlgorithm());
124: }
125: }
|