|
1 | 1 | // BZip2.cs |
2 | 2 | // |
3 | | -// Copyright (C) 2001 Mike Krueger |
| 3 | +// Copyright (C) 2010 David Pierson |
4 | 4 | // |
5 | 5 | // This program is free software; you can redistribute it and/or |
6 | 6 | // modify it under the terms of the GNU General Public License |
|
33 | 33 | // obligated to do so. If you do not wish to do so, delete this |
34 | 34 | // exception statement from your version. |
35 | 35 |
|
| 36 | +// Suppress this in CF and 1.1, not needed. Static classes introduced in C# version 2.0 |
| 37 | +#if !NETCF_2_0 && !NET_1_1 |
| 38 | + |
36 | 39 | using System; |
37 | 40 | using System.IO; |
38 | 41 |
|
39 | | -namespace ICSharpCode.SharpZipLib.BZip2 |
40 | | -{ |
| 42 | +namespace ICSharpCode.SharpZipLib.BZip2 { |
41 | 43 |
|
42 | 44 | /// <summary> |
43 | | - /// A helper class to simplify compressing and decompressing streams. |
| 45 | + /// An example class to demonstrate compression and decompression of BZip2 streams. |
44 | 46 | /// </summary> |
45 | | - public sealed class BZip2 |
| 47 | + public static class BZip2 |
46 | 48 | { |
47 | 49 | /// <summary> |
48 | | - /// Decompress <paramref name="inStream">input</paramref> writing |
49 | | - /// decompressed data to the <paramref name="outStream">output stream</paramref> |
| 50 | + /// Decompress the <paramref name="inStream">input</paramref> writing |
| 51 | + /// uncompressed data to the <paramref name="outStream">output stream</paramref> |
50 | 52 | /// </summary> |
51 | | - /// <param name="inStream">The stream containing data to decompress.</param> |
52 | | - /// <param name="outStream">The stream to write decompressed data to.</param> |
53 | | - /// <remarks>Both streams are closed on completion</remarks> |
54 | | - public static void Decompress(Stream inStream, Stream outStream) |
| 53 | + /// <param name="inStream">The readable stream containing data to decompress.</param> |
| 54 | + /// <param name="outStream">The output stream to receive the decompressed data.</param> |
| 55 | + /// <param name="isStreamOwner">Both streams are closed on completion if true.</param> |
| 56 | + public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner) |
55 | 57 | { |
56 | | - if ( inStream == null ) { |
57 | | - throw new ArgumentNullException("inStream"); |
58 | | - } |
59 | | - |
60 | | - if ( outStream == null ) { |
61 | | - throw new ArgumentNullException("outStream"); |
| 58 | + if (inStream == null || outStream == null) { |
| 59 | + throw new Exception("Null Stream"); |
62 | 60 | } |
63 | 61 |
|
64 | | - using ( outStream ) { |
65 | | - using ( BZip2InputStream bzis = new BZip2InputStream(inStream) ) { |
66 | | - int ch = bzis.ReadByte(); |
67 | | - while (ch != -1) { |
68 | | - outStream.WriteByte((byte)ch); |
69 | | - ch = bzis.ReadByte(); |
70 | | - } |
| 62 | + try { |
| 63 | + using (BZip2InputStream bzipInput = new BZip2InputStream(inStream)) { |
| 64 | + bzipInput.IsStreamOwner = isStreamOwner; |
| 65 | + Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]); |
| 66 | + } |
| 67 | + } finally { |
| 68 | + if (isStreamOwner) { |
| 69 | + // inStream is closed by the BZip2InputStream if stream owner |
| 70 | + outStream.Close(); |
71 | 71 | } |
72 | 72 | } |
73 | 73 | } |
74 | 74 |
|
75 | 75 | /// <summary> |
76 | | - /// Compress <paramref name="inStream">input stream</paramref> sending |
77 | | - /// result to <paramref name="outStream">output stream</paramref> |
| 76 | + /// Compress the <paramref name="inStream">input stream</paramref> sending |
| 77 | + /// result data to <paramref name="outStream">output stream</paramref> |
78 | 78 | /// </summary> |
79 | | - /// <param name="inStream">The stream to compress.</param> |
80 | | - /// <param name="outStream">The stream to write compressed data to.</param> |
81 | | - /// <param name="blockSize">The block size to use.</param> |
82 | | - /// <remarks>Both streams are closed on completion</remarks> |
83 | | - public static void Compress(Stream inStream, Stream outStream, int blockSize) |
84 | | - { |
85 | | - if ( inStream == null ) { |
86 | | - throw new ArgumentNullException("inStream"); |
87 | | - } |
88 | | - |
89 | | - if ( outStream == null ) { |
90 | | - throw new ArgumentNullException("outStream"); |
| 79 | + /// <param name="inStream">The readable stream to compress.</param> |
| 80 | + /// <param name="outStream">The output stream to receive the compressed data.</param> |
| 81 | + /// <param name="isStreamOwner">Both streams are closed on completion if true.</param> |
| 82 | + /// <param name="level">Block size acts as compression level (1 to 9) with 1 giving |
| 83 | + /// the lowest compression and 9 the highest.</param> |
| 84 | + public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level) |
| 85 | + { |
| 86 | + if (inStream == null || outStream == null) { |
| 87 | + throw new Exception("Null Stream"); |
91 | 88 | } |
92 | | - |
93 | | - using ( inStream ) { |
94 | | - using (BZip2OutputStream bzos = new BZip2OutputStream(outStream, blockSize)) { |
95 | | - int ch = inStream.ReadByte(); |
96 | | - while (ch != -1) { |
97 | | - bzos.WriteByte((byte)ch); |
98 | | - ch = inStream.ReadByte(); |
99 | | - } |
| 89 | + |
| 90 | + try { |
| 91 | + using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, level)) { |
| 92 | + bzipOutput.IsStreamOwner = isStreamOwner; |
| 93 | + Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]); |
| 94 | + } |
| 95 | + } finally { |
| 96 | + if (isStreamOwner) { |
| 97 | + // outStream is closed by the BZip2OutputStream if stream owner |
| 98 | + inStream.Close(); |
100 | 99 | } |
101 | 100 | } |
102 | 101 | } |
103 | 102 |
|
104 | | - /// <summary> |
105 | | - /// Initialise a default instance of this class. |
106 | | - /// </summary> |
107 | | - BZip2() |
108 | | - { |
109 | | - } |
110 | 103 | } |
111 | 104 | } |
112 | | - |
113 | | -/* derived from a file which contained this license : |
114 | | - * Copyright (c) 1999-2001 Keiron Liddle, Aftex Software |
115 | | - * |
116 | | - * This library is free software; you can redistribute it and/or |
117 | | - * modify it under the terms of the GNU Lesser General Public |
118 | | - * License as published by the Free Software Foundation; either |
119 | | - * version 2.1 of the License, or (at your option) any later version. |
120 | | - * |
121 | | - * This library is distributed in the hope that it will be useful, |
122 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
123 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
124 | | - * Lesser General Public License for more details. |
125 | | - * |
126 | | -*/ |
| 105 | +#endif |
0 commit comments