1 /*
   2  * Copyright (c) 2013, 2019, 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 
  32 /**
  33  * See {@link JITracer} for an explanation of this code.
  34  */
  35 @JIInstrumentationTarget("java.io.FileInputStream")
  36 final class FileInputStreamInstrumentor {
  37 
  38     private FileInputStreamInstrumentor() {
  39     }
  40 
  41     private String path;
  42 
  43     @SuppressWarnings("deprecation")
  44     @JIInstrumentationMethod
  45     public int read() throws IOException {
  46         FileReadEvent event = FileReadEvent.EVENT.get();
  47         if (!event.isEnabled()) {
  48             return read();
  49         }
  50         int result = 0;
  51         try {
  52             event.begin();
  53             result = read();
  54             if (result < 0) {
  55                 event.endOfFile = true;
  56             } else {
  57                 event.bytesRead = 1;
  58             }
  59         } finally {
  60             event.path = path;
  61             event.commit();
  62             event.reset();
  63         }
  64         return result;
  65     }
  66 
  67     @SuppressWarnings("deprecation")
  68     @JIInstrumentationMethod
  69     public int read(byte b[]) throws IOException {
  70         FileReadEvent event = FileReadEvent.EVENT.get();
  71         if (!event.isEnabled()) {
  72             return read(b);
  73         }
  74         int bytesRead = 0;
  75         try {
  76             event.begin();
  77             bytesRead = read(b);
  78         } finally {
  79             if (bytesRead < 0) {
  80                 event.endOfFile = true;
  81             } else {
  82                 event.bytesRead = bytesRead;
  83             }
  84             event.path = path;
  85             event.commit();
  86             event.reset();
  87         }
  88         return bytesRead;
  89     }
  90 
  91     @SuppressWarnings("deprecation")
  92     @JIInstrumentationMethod
  93     public int read(byte b[], int off, int len) throws IOException {
  94         FileReadEvent event = FileReadEvent.EVENT.get();
  95         if (!event.isEnabled()) {
  96             return read(b, off, len);
  97         }
  98         int bytesRead = 0;
  99         try {
 100             event.begin();
 101             bytesRead = read(b, off, len);
 102         } finally {
 103             if (bytesRead < 0) {
 104                 event.endOfFile = true;
 105             } else {
 106                 event.bytesRead = bytesRead;
 107             }
 108             event.path = path;
 109             event.commit();
 110             event.reset();
 111         }
 112         return bytesRead;
 113     }
 114 
 115 }