01: package org.jicarilla.http;
02:
03: import org.jicarilla.io.Filesystem;
04:
05: import java.io.IOException;
06: import java.nio.ByteBuffer;
07: import java.nio.channels.ReadableByteChannel;
08:
09: /**
10: *
11: *
12: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
13: * @version $Id: HTTPFileReaderImpl.java,v 1.2 2004/02/26 16:51:55 lsimons Exp $
14: */
15: public class HTTPFileReaderImpl implements HTTPFileReader {
16: public final static int BUFFER_SIZE = 8096;
17:
18: private Filesystem m_filesystem;
19:
20: public HTTPFileReaderImpl(final Filesystem fs) {
21: setFilesystem(fs);
22: }
23:
24: public Filesystem getFilesystem() {
25: return m_filesystem;
26: }
27:
28: public void setFilesystem(final Filesystem filesystem) {
29: m_filesystem = filesystem;
30: }
31:
32: public int readFile(final String file, final HTTPMessage res)
33: throws IOException {
34: final ReadableByteChannel rbc = getFilesystem().getFile(file);
35: int size = 0;
36: while (true) {
37: final ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
38: final int read = rbc.read(buf);
39: if (read < 0)
40: break;
41: if (read == 0)
42: continue;
43:
44: size += read;
45: buf.rewind();
46: buf.limit(read);
47: res.addBodyPart(buf);
48: }
49: getFilesystem().returnFile(rbc);
50: return size;
51: }
52: }
|