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