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;
028:
029: import java.io.EOFException;
030: import java.io.IOException;
031:
032: /**
033: Output stream default implementation.
034: */
035: public final class OutputStream implements IOutputStream {
036: // <class>
037: // <dynamic>
038: // <fields>
039: private java.io.OutputStream stream;
040:
041: // </fields>
042:
043: // <constructors>
044: public OutputStream(java.io.OutputStream stream) {
045: this .stream = stream;
046: }
047:
048: // </constructors>
049:
050: // <interface>
051: // <public>
052: // <IOutputStream>
053: public int write(byte[] data) {
054: try {
055: stream.write(data);
056: } catch (IOException e) {
057: throw new RuntimeException(e);
058: }
059:
060: return data.length;
061: }
062:
063: public int write(byte[] data, int offset, int length) {
064: try {
065: stream.write(data, offset, length);
066: } catch (IOException e) {
067: throw new RuntimeException(e);
068: }
069:
070: return length;
071: }
072:
073: public int write(String data) {
074: try {
075: stream.write(data.getBytes("ISO-8859-1"));
076: } catch (IOException e) {
077: throw new RuntimeException(e);
078: }
079:
080: return data.length();
081: }
082:
083: public int write(IInputStream data) {
084: try {
085: // TODO:IMPL bufferize in order to limit memory usage!!!
086: byte[] baseData = new byte[(int) data.getLength()];
087: // Force the source pointer to the BOF (as we must copy the entire content)!
088: data.seek(0);
089: // Read source content!
090: data.read(baseData);
091: // Write target content!
092: return write(baseData);
093: } catch (EOFException e) {
094: throw new RuntimeException(e);
095: }
096: }
097: // </IOutputStream>
098: // </public>
099: // </interface>
100: // </dynamic>
101: // </class>
102: }
|