ContentLengthOutputStreampublic class ContentLengthOutputStream extends OutputStream A stream wrapper that closes itself after a defined number of bytes. |
Fields Summary |
---|
private final SessionOutputBuffer | outWrapped session outbut buffer. | private final long | contentLengthThe maximum number of bytes that can be written the stream. Subsequent
write operations will be ignored. | private long | totalTotal bytes written | private boolean | closedTrue if the stream is closed. |
Constructors Summary |
---|
public ContentLengthOutputStream(SessionOutputBuffer out, long contentLength)Creates a new length limited stream
super();
if (out == null) {
throw new IllegalArgumentException("Session output buffer may not be null");
}
if (contentLength < 0) {
throw new IllegalArgumentException("Content length may not be negative");
}
this.out = out;
this.contentLength = contentLength;
|
Methods Summary |
---|
public void | close()Does not close the underlying socket output.
if (!this.closed) {
this.closed = true;
this.out.flush();
}
| public void | flush()
this.out.flush();
| public void | write(byte[] b, int off, int len)
if (this.closed) {
throw new IOException("Attempted write to closed stream.");
}
if (this.total < this.contentLength) {
long max = this.contentLength - this.total;
if (len > max) {
len = (int) max;
}
this.out.write(b, off, len);
this.total += len;
}
| public void | write(byte[] b)
write(b, 0, b.length);
| public void | write(int b)
if (this.closed) {
throw new IOException("Attempted write to closed stream.");
}
if (this.total < this.contentLength) {
this.out.write(b);
this.total++;
}
|
|