1 /*
   2  * Copyright (c) 2013, 2018, 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 jdk.jfr.internal.instrument;
  27 
  28 import java.io.IOException;
  29 
  30 import jdk.jfr.events.FileReadEvent;
  31 import jdk.jfr.events.FileWriteEvent;
  32 
  33 /**
  34  * See {@link JITracer} for an explanation of this code.
  35  */
  36 @JIInstrumentationTarget("java.io.RandomAccessFile")
  37 final class RandomAccessFileInstrumentor {
  38 
  39     private RandomAccessFileInstrumentor() {
  40     }
  41 
  42     private String path;
  43 
  44     @SuppressWarnings("deprecation")
  45     @JIInstrumentationMethod
  46     public int read() throws IOException {
  47         FileReadEvent event = FileReadEvent.EVENT.get();
  48         if (!event.isEnabled()) {
  49             return read();
  50         }
  51         int result = 0;
  52         try {
  53             event.begin();
  54             result = read();
  55             if (result < 0) {
  56                 event.endOfFile = true;
  57             } else {
  58                 event.bytesRead = 1;
  59             }
  60         } finally {
  61             event.path = path;
  62             event.commit();
  63             event.reset();
  64         }
  65         return result;
  66     }
  67 
  68     @SuppressWarnings("deprecation")
  69     @JIInstrumentationMethod
  70     public int read(byte b[]) throws IOException {
  71         FileReadEvent event = FileReadEvent.EVENT.get();
  72         if (!event.isEnabled()) {
  73             return read(b);
  74         }
  75         int bytesRead = 0;
  76         try {
  77             event.begin();
  78             bytesRead = read(b);
  79         } finally {
  80             if (bytesRead < 0) {
  81                 event.endOfFile = true;
  82             } else {
  83                 event.bytesRead = bytesRead;
  84             }
  85             event.path = path;
  86             event.commit();
  87             event.reset();
  88         }
  89         return bytesRead;
  90     }
  91 
  92     @SuppressWarnings("deprecation")
  93     @JIInstrumentationMethod
  94     public int read(byte b[], int off, int len) throws IOException {
  95         FileReadEvent event = FileReadEvent.EVENT.get();
  96         if (!event.isEnabled()) {
  97             return read(b, off, len);
  98         }
  99         int bytesRead = 0;
 100         try {
 101             event.begin();
 102             bytesRead = read(b, off, len);
 103         } finally {
 104             if (bytesRead < 0) {
 105                 event.endOfFile = true;
 106             } else {
 107                 event.bytesRead = bytesRead;
 108             }
 109             event.path = path;
 110             event.commit();
 111             event.reset();
 112         }
 113         return bytesRead;
 114     }
 115 
 116     @SuppressWarnings("deprecation")
 117     @JIInstrumentationMethod
 118     public void write(int b) throws IOException {
 119         FileWriteEvent event = FileWriteEvent.EVENT.get();
 120         if (!event.isEnabled()) {
 121             write(b);
 122             return;
 123         }
 124         try {
 125             event.begin();
 126             write(b);
 127             event.bytesWritten = 1;
 128         } finally {
 129             event.path = path;
 130             event.commit();
 131             event.reset();
 132         }
 133     }
 134 
 135     @SuppressWarnings("deprecation")
 136     @JIInstrumentationMethod
 137     public void write(byte b[]) throws IOException {
 138         FileWriteEvent event = FileWriteEvent.EVENT.get();
 139         if (!event.isEnabled()) {
 140             write(b);
 141             return;
 142         }
 143         try {
 144             event.begin();
 145             write(b);
 146             event.bytesWritten = b.length;
 147         } finally {
 148             event.path = path;
 149             event.commit();
 150             event.reset();
 151         }
 152     }
 153 
 154     @SuppressWarnings("deprecation")
 155     @JIInstrumentationMethod
 156     public void write(byte b[], int off, int len) throws IOException {
 157         FileWriteEvent event = FileWriteEvent.EVENT.get();
 158         if (!event.isEnabled()) {
 159             write(b, off, len);
 160             return;
 161         }
 162         try {
 163             event.begin();
 164             write(b, off, len);
 165             event.bytesWritten = len;
 166         } finally {
 167             event.path = path;
 168             event.commit();
 169             event.reset();
 170         }
 171     }
 172 
 173 }