1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
// java.io.FileOutputStream
// An implementation of the Java Language Specification section 20.16
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.
package java.io;
public class FileOutputStream extends OutputStream {
public FileOutputStream(String path)
throws SecurityException, FileNotFoundException
{
SecurityManager man=System.getSecurityManager();
if (man!=null) man.checkWrite(path);
fd=FileDescriptor.openwrite(path);
this.path=path;
}
public FileOutputStream(File f)
throws SecurityException, FileNotFoundException
{
this(f.getPath());
}
public FileOutputStream(FileDescriptor fd) throws SecurityException
{
SecurityManager man=System.getSecurityManager();
if (man!=null) man.checkWrite(fd.fd());
this.fd=fd;
}
private FileDescriptor fd;
private String path;
public final FileDescriptor getFD() throws IOException
{
return fd;
}
public void write(int b) throws IOException
{
byte[] buffer=new byte[1];
buffer[0]=(byte) b;
if (fd.write(buffer, 0, 1) != 1) {
throw new IOException();
}
}
public void write(byte[] b) throws IOException, NullPointerException
{
write(b, 0, b.length);
}
public void write(byte[] b, int off, int len)
throws IOException, NullPointerException,
IndexOutOfBoundsException
{
if (off<0 || len<0 || off+len>b.length)
throw new IndexOutOfBoundsException();
while (len>0) {
int written=fd.write(b, off, len);
if (written==0)
throw new IOException("Wrote zero bytes");
if (written<0) Errno.throwError(path);
off+=written;
len-=written;
}
}
public void close() throws IOException
{
if (fd.close()<0) Errno.throwError(path);
}
protected void finalize() throws IOException
{
close();
}
}
|