1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import java.net.InetAddress;
  29 
  30 /**
  31  * Utility class used to identify trace points for I/O calls.
  32  * <p>
  33  * To use this class, a diagnostic tool must redefine this class with a version
  34  * that contains calls to the the diagnostic tool. This implementation will then
  35  * receive callbacks when file and socket operations are performed. The reason
  36  * for requiring a redefine of the class is to avoid any overhead caused by the
  37  * instrumentation.
  38  * <p>
  39  * The xxBegin() methods return a "context". This can be any Object marked with
  40  * the {@link IoTraceContext} empty interface. This context will be passed to
  41  * the corresponding xxEnd() method. This way, an implementation can correlate
  42  * the beginning of an operation with the end.
  43  * <p>
  44  * It is possible for a xxEnd() method to be called with a null handle. This
  45  * happens if tracing was started between the call to xxBegin() and xxEnd(), in
  46  * which case xxBegin() would not have been called. It is the implementation's
  47  * responsibility to not throw an exception in this case.
  48  * <p>
  49  * Implementations must not throw exceptions since this will cause disruptions
  50  * to the I/O operations.
  51  * <p>
  52  * Implementations must not do I/O operations since this will lead to an endless
  53  * loop.
  54  * <p>
  55  * Only blocking I/O operations are identified with this facility.
  56  */
  57 public final class IoTrace {
  58     private IoTrace() {
  59     }
  60 
  61     /**
  62      * Called before data is read from a socket.
  63      *
  64      * @param address
  65      *            the remote address the socket is bound to
  66      * @param port
  67      *            the remote port the socket is bound to
  68      * @param timeout
  69      *            the SO_TIMEOUT value of the socket (in milliseconds) or 0 if
  70      *            there is no timeout set
  71      * @return an IoTraceContext object
  72      */
  73     public static IoTraceContext socketReadBegin(InetAddress address, int port,
  74             int timeout) {
  75         return null;
  76     }
  77 
  78     /**
  79      * Called after data is read from the socket.
  80      *
  81      * @param context
  82      *            the context returned by the previous call to socketReadBegin()
  83      * @param bytesRead
  84      *            the number of bytes read from the socket, 0 if there was an
  85      *            error reading from the socket
  86      */
  87     public static void socketReadEnd(IoTraceContext context, long bytesRead) {
  88     }
  89 
  90     /**
  91      * Called before data is written to a socket.
  92      *
  93      * @param address
  94      *            the remote address the socket is bound to
  95      * @param port
  96      *            the remote port the socket is bound to
  97      * @return an IoTraceContext object
  98      */
  99     public static IoTraceContext socketWriteBegin(InetAddress address, int port) {
 100         return null;
 101     }
 102 
 103     /**
 104      * Called after data is written to a socket.
 105      *
 106      * @param context
 107      *            the context returned by the previous call to
 108      *            socketWriteBegin()
 109      * @param bytesWritten
 110      *            the number of bytes written to the socket, 0 if there was an
 111      *            error writing to the socket
 112      */
 113     public static void socketWriteEnd(IoTraceContext context, long bytesWritten) {
 114     }
 115 
 116     /**
 117      * Called before data is read from a file.
 118      *
 119      * @param path
 120      *            the path of the file
 121      * @return an IoTraceContext object
 122      */
 123     public static IoTraceContext fileReadBegin(String path) {
 124         return null;
 125     }
 126 
 127     /**
 128      * Called after data is read from a file.
 129      *
 130      * @param context
 131      *            the context returned by the previous call to fileReadBegin()
 132      * @param bytesRead
 133      *            the number of bytes written to the file, 0 if there was an
 134      *            error writing to the file
 135      */
 136     public static void fileReadEnd(IoTraceContext context, long bytesRead) {
 137     }
 138 
 139     /**
 140      * Called before data is written to a file.
 141      *
 142      * @param path
 143      *            the path of the file
 144      * @return an IoTraceContext object
 145      */
 146     public static IoTraceContext fileWriteBegin(String path) {
 147         return null;
 148     }
 149 
 150     /**
 151      * Called after data is written to a file.
 152      *
 153      * @param context
 154      *            the context returned by the previous call to fileReadBegin()
 155      * @param bytesWritten
 156      *            the number of bytes written to the file, 0 if there was an
 157      *            error writing to the file
 158      */
 159     public static void fileWriteEnd(IoTraceContext context, long bytesWritten) {
 160     }
 161 }