1 /*
   2  * Copyright (c) 2014 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 package org.openjdk.bench.javax.xml;
  26 
  27 import org.openjdk.jmh.annotations.Param;
  28 import org.openjdk.jmh.annotations.Scope;
  29 import org.openjdk.jmh.annotations.State;
  30 
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import java.net.URISyntaxException;
  35 import java.util.concurrent.ConcurrentHashMap;
  36 
  37 /**
  38  * Abstract base class with functionality and constants used by the XML micros.
  39  */
  40 @State(Scope.Benchmark)
  41 public abstract class AbstractXMLMicro {
  42 
  43   public static final String BUILDIMPL = "build-impl.xml";
  44   public static final String LOGCOMP = "log_comp.xml";
  45   public static final String MESSAGE12 = "message_12.xml";
  46   public static final String MSGATTACH = "msgAttach.xml";
  47   public static final String REZ = "reZ003vExc23082309.xml";
  48 
  49   protected static final ConcurrentHashMap<String, byte[]> byteCache = new ConcurrentHashMap<>();
  50 
  51   @Param({BUILDIMPL,LOGCOMP,MESSAGE12,MSGATTACH,REZ})
  52   protected String doc;
  53 
  54   /**
  55    * Gets a given InputStream as a byte-array.
  56    *
  57    * @param is stream to read from
  58    * @return byte-array
  59    * @throws IOException if things go crazy-crazy
  60    */
  61   private static byte[] getBytes(InputStream is) throws IOException {
  62     ByteArrayOutputStream baos = new ByteArrayOutputStream();
  63     byte[] b = new byte[1024];
  64 
  65     int available = is.available();
  66     while (available > 0) {
  67       int read = Math.min(b.length, available);
  68 
  69       int actuallyRead = is.read(b, 0, read);
  70       baos.write(b, 0, actuallyRead);
  71 
  72       available = is.available();
  73     }
  74 
  75     is.close();
  76     byte array[] = baos.toByteArray();
  77     baos.close();
  78 
  79     return array;
  80   }
  81 
  82   /**
  83    * Gets a given resource as a byte-array.
  84    *
  85    * @param name resource to fetch
  86    * @return byte-array
  87    * @throws IOException if things go crazy-crazy
  88    * @throws URISyntaxException if resource given doesn't match syntax
  89    */
  90   protected byte[] getFileBytesFromResource(String name) throws IOException, URISyntaxException {
  91     byte[] bytes = byteCache.get(name);
  92     if (bytes == null) {
  93       bytes = getBytes(this.getClass().getResourceAsStream("/"
  94               + AbstractXMLMicro.class.getPackage().getName().replace(".", "/")
  95               + "/" + name));
  96       byteCache.put(name, bytes);
  97     }
  98     return bytes;
  99   }
 100 
 101 }