FileDocCategorySizeDatePackage
FileContent.javaAPI DocSun JDK 1.5.0 Example3536Sat Jan 08 15:08:43 GMT 2005None

FileContent

public class FileContent extends Object implements Content
A Content type that provides for transferring files.
author
Mark Reinhold
author
Brad R. Wetmore
version
1.2, 04/07/26

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 longlength()

		// NB only; >= 0 if transferring

       
	return length;
    
public voidprepare()

	if (fc == null)
	    fc = new RandomAccessFile(fn, "r").getChannel();
	length = fc.size();
	position = 0;			// NB only
    
public voidrelease()

	if (fc != null) {
	    fc.close();
	    fc = null;
	}
    
public booleansend(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.Stringtype()


       
	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;