KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > bytes > filters > FlateFilter


1 /*
2   Copyright © 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright © 2006 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.bytes.filters;
28
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.util.zip.DeflaterOutputStream JavaDoc;
35 import java.util.zip.InflaterInputStream JavaDoc;
36
37 /**
38   zlib/deflate [RFC:1950,1951] filter [PDF:1.6:3.3.3].
39
40   @author Stefano Chizzolini
41   @version 0.0.2, 12/19/06
42   @since 0.0.2
43 */

44 public class FlateFilter
45   extends Filter
46 {
47   // <class>
48
// <dynamic>
49
// <constructors>
50
FlateFilter(
51     )
52   {}
53   // </constructors>
54

55   // <interface>
56
// <public>
57
public byte[] decode(
58     byte[] data,
59     int offset,
60     int length
61     )
62   {
63     ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
64     try
65     {
66       InflaterInputStream JavaDoc inputFilter = new InflaterInputStream JavaDoc(
67         new ByteArrayInputStream JavaDoc(data,offset,length)
68         );
69
70       transform(inputFilter,outputStream);
71     }
72     catch(IOException JavaDoc e)
73     {throw new RuntimeException JavaDoc("Decoding failed.",e);}
74
75     return outputStream.toByteArray();
76   }
77
78   public byte[] encode(
79     byte[] data,
80     int offset,
81     int length
82     )
83   {
84     ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
85     try
86     {
87       DeflaterOutputStream JavaDoc outputFilter = new DeflaterOutputStream JavaDoc(outputStream);
88       ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(data,offset,length);
89
90       transform(inputStream,outputFilter);
91     }
92     catch(IOException JavaDoc e)
93     {throw new RuntimeException JavaDoc("Encoding failed.",e);}
94
95     return outputStream.toByteArray();
96   }
97   // </public>
98

99   // <private>
100
private void transform(
101     InputStream JavaDoc input,
102     OutputStream JavaDoc output
103     )
104     throws IOException JavaDoc
105   {
106     byte[] buffer = new byte[8192]; int bufferLength;
107     while((bufferLength = input.read(buffer, 0, buffer.length)) != -1)
108     {output.write(buffer, 0, bufferLength);}
109
110     input.close(); output.close();
111   }
112   // </private>
113
// </interface>
114
// </dynamic>
115
// </class>
116
}
Popular Tags