org.wings.io.OutputStreamDevice Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
package org.wings.io;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* A Device encapsulating a ServletOutputStream
*
* @author Holger Engels
*/
public final class OutputStreamDevice implements Device {
private PrintStream out;
public OutputStreamDevice(OutputStream out) {
this.out = new PrintStream(out);
}
@Override
public boolean isSizePreserving() { return true; }
/**
* Flush this Stream.
*/
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
out.close();
}
/**
* Print a String.
*/
@Override
public Device print(String s) throws IOException {
if (s == null)
out.print("null");
else
out.print(s);
return this;
}
/**
* Print an integer.
*/
@Override
public Device print(int i) throws IOException {
out.print(i);
return this;
}
/**
* Print any Object
*/
@Override
public Device print(Object o) throws IOException {
if (o == null)
out.print("null");
else
out.print(o.toString());
return this;
}
/**
* Print a character.
*/
@Override
public Device print(char c) throws IOException {
out.print(c);
return this;
}
/**
* Print an array of chars.
*/
@Override
public Device print(char... c) throws IOException {
return print(c, 0, c.length - 1);
}
/**
* Print a character array.
*/
@Override
public Device print(char[] c, int start, int len) throws IOException {
final int end = start + len;
for (int i = start; i < end; ++i)
out.print(c[i]);
return this;
}
/**
* Writes the specified byte to this data output stream.
*/
@Override
public Device write(int c) throws IOException {
out.write(c);
return this;
}
/**
* Writes b.length bytes from the specified byte array to this
* output stream.
*/
@Override
public Device write(byte... b) throws IOException {
out.write(b);
return this;
}
/**
* Writes len bytes from the specified byte array starting at offset
* off to this output stream.
*/
@Override
public Device write(byte b[], int off, int len) throws IOException {
out.write(b, off, len);
return this;
}
}