< prev index next >

test/jdk/java/lang/instrument/NamedBuffer.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  67         {
  68         // hack for now, just assume the stream will fit in our reasonable size buffer.
  69         // if not, panic
  70         int bufferLimit = 200 * 1024;
  71         byte[]  readBuffer = new byte[bufferLimit];
  72         int actualSize = stream.read(readBuffer);
  73         if ( actualSize >= bufferLimit )
  74             {
  75             // if there might be more bytes, just surrender
  76             throw new IOException("too big for buffer");
  77             }
  78 
  79         byte[] resultBuffer = new byte[actualSize];
  80         System.arraycopy(   readBuffer,
  81                             0,
  82                             resultBuffer,
  83                             0,
  84                             actualSize);
  85         return resultBuffer;
  86         }























































  87 }
   1 /*
   2  * Copyright (c) 2003, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  67     {
  68         // hack for now, just assume the stream will fit in our reasonable size buffer.
  69         // if not, panic
  70         int bufferLimit = 200 * 1024;
  71         byte[]  readBuffer = new byte[bufferLimit];
  72         int actualSize = stream.read(readBuffer);
  73         if ( actualSize >= bufferLimit )
  74             {
  75             // if there might be more bytes, just surrender
  76             throw new IOException("too big for buffer");
  77             }
  78 
  79         byte[] resultBuffer = new byte[actualSize];
  80         System.arraycopy(   readBuffer,
  81                             0,
  82                             resultBuffer,
  83                             0,
  84                             actualSize);
  85         return resultBuffer;
  86     }
  87 
  88     static final String DEST = System.getProperty("test.classes");
  89     static final boolean VERBOSE = false;
  90 
  91     static boolean checkMatch(byte[] buf, byte[] name, int begIdx) {
  92         if (buf.length < name.length + begIdx) {
  93             return false;
  94         }
  95         for (int i = 0; i < name.length; i++) {
  96             if (buf[i + begIdx] != name[i]) {
  97                 return false;
  98             }
  99         }
 100         return true;
 101     }
 102 
 103     // This function reads a class file from disk and replaces the first character of
 104     // the name with the one passed as "replace".  Then goes through the bytecodes to
 105     // replace all instances of the name of the class with the new name.  The
 106     // redefinition tests use this to redefine Host$ classes with precompiled class files
 107     // Xost.java, Yost.java and Zost.java.
 108     static byte[]
 109     bytesForHostClass(char replace, String className) throws Throwable {
 110         String tail = className.substring(1);
 111         String origClassName = "" + replace + tail;
 112         File clsfile = new File(DEST + "/" + origClassName + ".class");
 113 
 114         if (VERBOSE) {
 115             System.out.println("   Reading bytes from " + clsfile);
 116         }
 117         byte[] buf = null;
 118         try (FileInputStream str = new FileInputStream(clsfile)) {
 119             buf = loadBufferFromStream(str);
 120         }
 121 
 122         boolean found = false;
 123         int dollarSignIdx = className.indexOf('$');
 124         int ptrnLen = (dollarSignIdx == -1) ? className.length() : dollarSignIdx;
 125         byte[] ptrnBytes = origClassName.substring(0, ptrnLen).getBytes();
 126         byte firstByte = className.getBytes()[0];
 127 
 128         for (int i = 0; i < buf.length - ptrnLen; i++) {
 129             if (checkMatch(buf, ptrnBytes, i)) {
 130                 if (VERBOSE) {
 131                     System.out.println("Appear to have found " + origClassName + " starting at " + i);
 132                 }
 133                 buf[i] = firstByte;
 134                 found = true;
 135             }
 136         }
 137         if (!found) {
 138             throw new Error("Could not locate '" + ptrnBytes + "' name in byte array");
 139         }
 140         return buf;
 141     }
 142 }
< prev index next >