001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.io;
014:
015: import javax.servlet.ServletOutputStream;
016:
017: import java.io.BufferedWriter;
018: import java.io.IOException;
019: import java.io.OutputStreamWriter;
020: import java.io.Writer;
021:
022: /**
023: * A Device encapsulating a ServletOutputStream.
024: *
025: * @author <a href="mailto:H.Zeller@acm.org">Henner Zeller</a>
026: */
027: public final class ServletDevice implements Device {
028: private final ServletOutputStream out;
029: private final Writer writer;
030:
031: public ServletDevice(ServletOutputStream out, String encoding)
032: throws IOException {
033: this .out = out;
034: writer = new BufferedWriter(new OutputStreamWriter(out,
035: encoding));
036: }
037:
038: public boolean isSizePreserving() {
039: return true;
040: }
041:
042: /**
043: * Flush this Stream.
044: */
045: public void flush() throws IOException {
046: writer.flush();
047: }
048:
049: public void close() throws IOException {
050: writer.flush();
051: out.close();
052: }
053:
054: /**
055: * Print a String.
056: */
057: public Device print(String s) throws IOException {
058: if (s == null)
059: writer.write("null");
060: else
061: writer.write(s);
062: return this ;
063: }
064:
065: /**
066: * Print an integer.
067: */
068: public Device print(int i) throws IOException {
069: print(String.valueOf(i));
070: return this ;
071: }
072:
073: /**
074: * Print any Object
075: */
076: public Device print(Object o) throws IOException {
077: if (o == null)
078: print("null");
079: else
080: print(o.toString());
081: return this ;
082: }
083:
084: /**
085: * Print a character.
086: */
087: public Device print(char c) throws IOException {
088: writer.write(c);
089: return this ;
090: }
091:
092: /**
093: * Print an array of chars.
094: */
095: public Device print(char[] c) throws IOException {
096: writer.write(c);
097: return this ;
098: }
099:
100: /**
101: * Print a character array.
102: */
103: public Device print(char[] c, int start, int len)
104: throws IOException {
105: writer.write(c, start, len);
106: return this ;
107: }
108:
109: /**
110: * Writes the specified byte to this data output stream.
111: */
112: public Device write(int c) throws IOException {
113: // This method is expensive.
114: writer.flush();
115: out.write(c);
116: out.flush();
117: return this ;
118: }
119:
120: /**
121: * Writes b.length bytes from the specified byte array to this
122: * output stream.
123: */
124: public Device write(byte b[]) throws IOException {
125: // This method is expensive.
126: writer.flush();
127: out.write(b);
128: out.flush();
129: return this ;
130: }
131:
132: /**
133: * Writes len bytes from the specified byte array starting at offset
134: * off to this output stream.
135: */
136: public Device write(byte b[], int off, int len) throws IOException {
137: // This method is expensive.
138: writer.flush();
139: out.write(b, off, len);
140: out.flush();
141: return this;
142: }
143: }
|