1 7 8 package java.io; 9 10 import java.nio.channels.FileChannel ; 11 import sun.nio.ch.FileChannelImpl; 12 13 14 34 public 35 class FileOutputStream extends OutputStream 36 { 37 42 private FileDescriptor fd; 43 44 private FileChannel channel= null; 45 46 private boolean append = false; 47 48 69 public FileOutputStream(String name) throws FileNotFoundException { 70 this(name != null ? new File (name) : null, false); 71 } 72 73 99 public FileOutputStream(String name, boolean append) 100 throws FileNotFoundException 101 { 102 this(name != null ? new File (name) : null, append); 103 } 104 105 130 public FileOutputStream(File file) throws FileNotFoundException { 131 this(file, false); 132 } 133 134 163 public FileOutputStream(File file, boolean append) 164 throws FileNotFoundException 165 { 166 String name = (file != null ? file.getPath() : null); 167 SecurityManager security = System.getSecurityManager(); 168 if (security != null) { 169 security.checkWrite(name); 170 } 171 if (name == null) { 172 throw new NullPointerException (); 173 } 174 fd = new FileDescriptor (); 175 this.append = append; 176 if (append) { 177 openAppend(name); 178 } else { 179 open(name); 180 } 181 } 182 183 198 public FileOutputStream(FileDescriptor fdObj) { 199 SecurityManager security = System.getSecurityManager(); 200 if (fdObj == null) { 201 throw new NullPointerException (); 202 } 203 if (security != null) { 204 security.checkWrite(fdObj); 205 } 206 fd = fdObj; 207 } 208 209 213 private native void open(String name) throws FileNotFoundException ; 214 215 219 private native void openAppend(String name) throws FileNotFoundException ; 220 221 228 public native void write(int b) throws IOException ; 229 230 237 private native void writeBytes(byte b[], int off, int len) throws IOException ; 238 239 246 public void write(byte b[]) throws IOException { 247 writeBytes(b, 0, b.length); 248 } 249 250 259 public void write(byte b[], int off, int len) throws IOException { 260 writeBytes(b, off, len); 261 } 262 263 276 public void close() throws IOException { 277 if (channel != null) 278 channel.close(); 279 close0(); 280 } 281 282 292 public final FileDescriptor getFD() throws IOException { 293 if (fd != null) return fd; 294 throw new IOException (); 295 } 296 297 314 public FileChannel getChannel() { 315 synchronized (this) { 316 if (channel == null) 317 channel = FileChannelImpl.open(fd, false, true, this, append); 318 return channel; 319 } 320 } 321 322 330 protected void finalize() throws IOException { 331 if (fd != null) { 332 if (fd == fd.out || fd == fd.err) { 333 flush(); 334 } else { 335 close(); 336 } 337 } 338 } 339 340 private native void close0() throws IOException ; 341 342 private static native void initIDs(); 343 344 static { 345 initIDs(); 346 } 347 348 } 349 | Popular Tags |