You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.1 KiB
119 lines
4.1 KiB
13 years ago
|
//httpChunkedOutputStream.java
|
||
19 years ago
|
//-------------------------------------
|
||
|
//part of YACY
|
||
17 years ago
|
//(C) by Michael Peter Christen; mc@yacy.net
|
||
19 years ago
|
//first published on http://www.anomic.de
|
||
|
//Frankfurt, Germany, 2004
|
||
|
//
|
||
|
//This file ist contributed by Martin Thelian
|
||
|
//last major change: 05.09.2005
|
||
|
//
|
||
|
//This program is free software; you can redistribute it and/or modify
|
||
|
//it under the terms of the GNU General Public License as published by
|
||
|
//the Free Software Foundation; either version 2 of the License, or
|
||
|
//(at your option) any later version.
|
||
|
//
|
||
|
//This program is distributed in the hope that it will be useful,
|
||
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
//GNU General Public License for more details.
|
||
|
//
|
||
|
//You should have received a copy of the GNU General Public License
|
||
|
//along with this program; if not, write to the Free Software
|
||
|
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
12 years ago
|
package net.yacy.server.http;
|
||
20 years ago
|
|
||
|
import java.io.FilterOutputStream;
|
||
|
import java.io.IOException;
|
||
18 years ago
|
import java.io.InputStream;
|
||
20 years ago
|
import java.io.OutputStream;
|
||
|
|
||
14 years ago
|
import net.yacy.cora.document.ASCII;
|
||
14 years ago
|
import net.yacy.cora.document.UTF8;
|
||
15 years ago
|
import net.yacy.kelondro.util.ByteBuffer;
|
||
|
import net.yacy.kelondro.util.FileUtils;
|
||
12 years ago
|
import net.yacy.server.serverCore;
|
||
18 years ago
|
|
||
16 years ago
|
public final class ChunkedOutputStream extends FilterOutputStream {
|
||
13 years ago
|
private boolean finished = false;
|
||
|
|
||
16 years ago
|
public ChunkedOutputStream(final OutputStream out) {
|
||
20 years ago
|
super(out);
|
||
|
}
|
||
13 years ago
|
|
||
|
@Override
|
||
|
public synchronized void close() throws IOException {
|
||
20 years ago
|
if (!this.finished) this.finish();
|
||
|
this.out.close();
|
||
|
}
|
||
13 years ago
|
|
||
20 years ago
|
public void finish() throws IOException {
|
||
|
if (!this.finished) {
|
||
18 years ago
|
this.out.write((byte) 48);
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
|
this.out.write(serverCore.CRLF);
|
||
20 years ago
|
this.out.flush();
|
||
|
this.finished = true;
|
||
|
}
|
||
|
}
|
||
13 years ago
|
|
||
|
@Override
|
||
17 years ago
|
public void write(final byte[] b) throws IOException {
|
||
13 years ago
|
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||
20 years ago
|
if (b.length == 0) return;
|
||
13 years ago
|
|
||
14 years ago
|
this.out.write(ASCII.getBytes(Integer.toHexString(b.length)));
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
20 years ago
|
this.out.write(b);
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
20 years ago
|
this.out.flush();
|
||
|
}
|
||
13 years ago
|
|
||
|
@Override
|
||
17 years ago
|
public void write(final byte[] b, final int off, final int len) throws IOException {
|
||
20 years ago
|
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||
|
if (len == 0) return;
|
||
13 years ago
|
|
||
14 years ago
|
this.out.write(ASCII.getBytes(Integer.toHexString(len)));
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
20 years ago
|
this.out.write(b, off, len);
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
18 years ago
|
this.out.flush();
|
||
|
}
|
||
13 years ago
|
|
||
16 years ago
|
public void write(final ByteBuffer b, final int off, final int len) throws IOException {
|
||
18 years ago
|
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||
|
if (len == 0) return;
|
||
13 years ago
|
|
||
14 years ago
|
this.out.write(ASCII.getBytes(Integer.toHexString(len)));
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
18 years ago
|
this.out.write(b.getBytes(off, len));
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
18 years ago
|
this.out.flush();
|
||
|
}
|
||
13 years ago
|
|
||
17 years ago
|
public void write(final InputStream b) throws IOException {
|
||
18 years ago
|
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||
17 years ago
|
final int len = b.available();
|
||
18 years ago
|
if (len == 0) return;
|
||
13 years ago
|
|
||
14 years ago
|
this.out.write(ASCII.getBytes(Integer.toHexString(len)));
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
13 years ago
|
FileUtils.copy(b, this.out, len);
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
20 years ago
|
this.out.flush();
|
||
|
}
|
||
13 years ago
|
|
||
|
@Override
|
||
17 years ago
|
public void write(final int b) throws IOException {
|
||
20 years ago
|
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||
13 years ago
|
|
||
14 years ago
|
this.out.write(UTF8.getBytes("1"));
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
20 years ago
|
this.out.write(b);
|
||
17 years ago
|
this.out.write(serverCore.CRLF);
|
||
20 years ago
|
this.out.flush();
|
||
|
}
|
||
|
}
|