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.FileWriteEvent;
  31 
  32 /**
  33  * See {@link JITracer} for an explanation of this code.
  34  */
  35 @JIInstrumentationTarget("java.io.FileOutputStream")
  36 final class FileOutputStreamInstrumentor {
  37 
  38     private FileOutputStreamInstrumentor() {
  39     }
  40 
  41     private String path;
  42 
  43     @SuppressWarnings("deprecation")
  44     @JIInstrumentationMethod
  45     public void write(int b) throws IOException {
  46         FileWriteEvent event = FileWriteEvent.EVENT.get();
  47         if (!event.isEnabled()) {
  48             write(b);
  49             return;
  50         }
  51         try {
  52             event.begin();
  53             write(b);
  54             event.bytesWritten = 1;
  55         } finally {
  56             event.path = path;
  57             event.commit();
  58             event.reset();
  59         }
  60     }
  61 
  62     @SuppressWarnings("deprecation")
  63     @JIInstrumentationMethod
  64     public void write(byte b[]) throws IOException {
  65         FileWriteEvent event = FileWriteEvent.EVENT.get();
  66         if (!event.isEnabled()) {
  67             write(b);
  68             return;
  69         }
  70         try {
  71             event.begin();
  72             write(b);
  73             event.bytesWritten = b.length;
  74         } finally {
  75             event.path = path;
  76             event.commit();
  77             event.reset();
  78         }
  79     }
  80 
  81     @SuppressWarnings("deprecation")
  82     @JIInstrumentationMethod
  83     public void write(byte b[], int off, int len) throws IOException {
  84         FileWriteEvent event = FileWriteEvent.EVENT.get();
  85         if (!event.isEnabled()) {
  86             write(b, off, len);
  87             return;
  88         }
  89         try {
  90             event.begin();
  91             write(b, off, len);
  92             event.bytesWritten = len;
  93         } finally {
  94             event.path = path;
  95             event.commit();
  96             event.reset();
  97         }
  98     }
  99 }