FileContentpublic class FileContent extends Object implements ContentA Content type that provides for transferring files. |
Fields Summary |
---|
private static File | ROOT | private File | fn | private String | type | private FileChannel | fc | private long | length | private long | position |
Constructors Summary |
---|
FileContent(URI uri)
fn = new File(ROOT,
uri.getPath()
.replace('/",
File.separatorChar));
|
Methods Summary |
---|
public long | length() // NB only; >= 0 if transferring
return length;
| public void | prepare()
if (fc == null)
fc = new RandomAccessFile(fn, "r").getChannel();
length = fc.size();
position = 0; // NB only
| public void | release()
if (fc != null) {
fc.close();
fc = null;
}
| public boolean | send(ChannelIO cio)
if (fc == null)
throw new IllegalStateException();
if (position < 0) // NB only
throw new IllegalStateException();
/*
* Short-circuit if we're already done.
*/
if (position >= length) {
return false;
}
position += cio.transferTo(fc, position, length - position);
return (position < length);
| public java.lang.String | type()
if (type != null)
return type;
String nm = fn.getName();
if (nm.endsWith(".html"))
type = "text/html; charset=iso-8859-1";
else if ((nm.indexOf('.") < 0) || nm.endsWith(".txt"))
type = "text/plain; charset=iso-8859-1";
else
type = "application/octet-stream";
return type;
|
|