001: /*
002: Copyright © 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.bytes.filters;
028:
029: import java.io.ByteArrayInputStream;
030: import java.io.ByteArrayOutputStream;
031: import java.io.InputStream;
032: import java.io.IOException;
033: import java.io.OutputStream;
034: import java.util.zip.DeflaterOutputStream;
035: import java.util.zip.InflaterInputStream;
036:
037: /**
038: zlib/deflate [RFC:1950,1951] filter [PDF:1.6:3.3.3].
039:
040: @author Stefano Chizzolini
041: @version 0.0.2, 12/19/06
042: @since 0.0.2
043: */
044: public class FlateFilter extends Filter {
045: // <class>
046: // <dynamic>
047: // <constructors>
048: FlateFilter() {
049: }
050:
051: // </constructors>
052:
053: // <interface>
054: // <public>
055: public byte[] decode(byte[] data, int offset, int length) {
056: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
057: try {
058: InflaterInputStream inputFilter = new InflaterInputStream(
059: new ByteArrayInputStream(data, offset, length));
060:
061: transform(inputFilter, outputStream);
062: } catch (IOException e) {
063: throw new RuntimeException("Decoding failed.", e);
064: }
065:
066: return outputStream.toByteArray();
067: }
068:
069: public byte[] encode(byte[] data, int offset, int length) {
070: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
071: try {
072: DeflaterOutputStream outputFilter = new DeflaterOutputStream(
073: outputStream);
074: ByteArrayInputStream inputStream = new ByteArrayInputStream(
075: data, offset, length);
076:
077: transform(inputStream, outputFilter);
078: } catch (IOException e) {
079: throw new RuntimeException("Encoding failed.", e);
080: }
081:
082: return outputStream.toByteArray();
083: }
084:
085: // </public>
086:
087: // <private>
088: private void transform(InputStream input, OutputStream output)
089: throws IOException {
090: byte[] buffer = new byte[8192];
091: int bufferLength;
092: while ((bufferLength = input.read(buffer, 0, buffer.length)) != -1) {
093: output.write(buffer, 0, bufferLength);
094: }
095:
096: input.close();
097: output.close();
098: }
099: // </private>
100: // </interface>
101: // </dynamic>
102: // </class>
103: }
|