src/share/classes/java/io/FileOutputStream.java

Print this page
rev 5501 : imported patch io-trace

@@ -25,10 +25,11 @@
 
 package java.io;
 
 import java.nio.channels.FileChannel;
 import sun.nio.ch.FileChannelImpl;
+import sun.misc.IoTrace;
 
 
 /**
  * A file output stream is an output stream for writing data to a
  * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not

@@ -56,10 +57,15 @@
      * The system dependent file descriptor.
      */
     private final FileDescriptor fd;
 
     /**
+     * The path of the referenced file (null if the stream is created with a file descriptor)
+     */
+    private final String path;
+
+    /**
      * True if the file is opened for append.
      */
     private final boolean append;
 
     /**

@@ -205,11 +211,11 @@
         if (name == null) {
             throw new NullPointerException();
         }
         this.fd = new FileDescriptor();
         this.append = append;
-
+        this.path = name;
         fd.incrementAndGetUseCount();
         open(name, append);
     }
 
     /**

@@ -242,10 +248,11 @@
         }
         if (security != null) {
             security.checkWrite(fdObj);
         }
         this.fd = fdObj;
+        this.path = null;
         this.append = false;
 
         /*
          * FileDescriptor is being shared by streams.
          * Ensure that it's GC'ed only when all the streams/channels are done

@@ -277,11 +284,18 @@
      *
      * @param      b   the byte to be written.
      * @exception  IOException  if an I/O error occurs.
      */
     public void write(int b) throws IOException {
+        Object traceContext = IoTrace.fileWriteBegin(path);
+        int bytesWritten = 0;
+        try {
         write(b, append);
+            bytesWritten = 1;
+        } finally {
+            IoTrace.fileWriteEnd(traceContext, bytesWritten);
+        }
     }
 
     /**
      * Writes a sub array as a sequence of bytes.
      * @param b the data to be written

@@ -300,11 +314,18 @@
      *
      * @param      b   the data.
      * @exception  IOException  if an I/O error occurs.
      */
     public void write(byte b[]) throws IOException {
+        Object traceContext = IoTrace.fileWriteBegin(path);
+        int bytesWritten = 0;
+        try {
         writeBytes(b, 0, b.length, append);
+            bytesWritten = b.length;
+        } finally {
+            IoTrace.fileWriteEnd(traceContext, bytesWritten);
+        }
     }
 
     /**
      * Writes <code>len</code> bytes from the specified byte array
      * starting at offset <code>off</code> to this file output stream.

@@ -313,11 +334,18 @@
      * @param      off   the start offset in the data.
      * @param      len   the number of bytes to write.
      * @exception  IOException  if an I/O error occurs.
      */
     public void write(byte b[], int off, int len) throws IOException {
+        Object traceContext = IoTrace.fileWriteBegin(path);
+        int bytesWritten = 0;
+        try {
         writeBytes(b, off, len, append);
+            bytesWritten = len;
+        } finally {
+            IoTrace.fileWriteEnd(traceContext, bytesWritten);
+        }
     }
 
     /**
      * Closes this file output stream and releases any system resources
      * associated with this stream. This file output stream may no longer

@@ -396,11 +424,11 @@
      * @spec JSR-51
      */
     public FileChannel getChannel() {
         synchronized (this) {
             if (channel == null) {
-                channel = FileChannelImpl.open(fd, false, true, append, this);
+                channel = FileChannelImpl.open(fd, path, false, true, append, this);
 
                 /*
                  * Increment fd's use count. Invoking the channel's close()
                  * method will result in decrementing the use count set for
                  * the channel.