Path: blob/master/test/micro/org/openjdk/bench/javax/xml/AbstractXMLMicro.java
41159 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.javax.xml;2324import org.openjdk.jmh.annotations.Param;25import org.openjdk.jmh.annotations.Scope;26import org.openjdk.jmh.annotations.State;2728import java.io.ByteArrayOutputStream;29import java.io.IOException;30import java.io.InputStream;31import java.net.URISyntaxException;32import java.util.concurrent.ConcurrentHashMap;3334/**35* Abstract base class with functionality and constants used by the XML micros.36*/37@State(Scope.Benchmark)38public abstract class AbstractXMLMicro {3940public static final String BUILDIMPL = "build-impl.xml";41public static final String LOGCOMP = "log_comp.xml";42public static final String MESSAGE12 = "message_12.xml";43public static final String MSGATTACH = "msgAttach.xml";44public static final String REZ = "reZ003vExc23082309.xml";4546protected static final ConcurrentHashMap<String, byte[]> byteCache = new ConcurrentHashMap<>();4748@Param({BUILDIMPL,LOGCOMP,MESSAGE12,MSGATTACH,REZ})49protected String doc;5051/**52* Gets a given InputStream as a byte-array.53*54* @param is stream to read from55* @return byte-array56* @throws IOException if things go crazy-crazy57*/58private static byte[] getBytes(InputStream is) throws IOException {59ByteArrayOutputStream baos = new ByteArrayOutputStream();60byte[] b = new byte[1024];6162int available = is.available();63while (available > 0) {64int read = Math.min(b.length, available);6566int actuallyRead = is.read(b, 0, read);67baos.write(b, 0, actuallyRead);6869available = is.available();70}7172is.close();73byte array[] = baos.toByteArray();74baos.close();7576return array;77}7879/**80* Gets a given resource as a byte-array.81*82* @param name resource to fetch83* @return byte-array84* @throws IOException if things go crazy-crazy85* @throws URISyntaxException if resource given doesn't match syntax86*/87protected byte[] getFileBytesFromResource(String name) throws IOException, URISyntaxException {88byte[] bytes = byteCache.get(name);89if (bytes == null) {90bytes = getBytes(this.getClass().getResourceAsStream("/"91+ AbstractXMLMicro.class.getPackage().getName().replace(".", "/")92+ "/" + name));93byteCache.put(name, bytes);94}95return bytes;96}9798}99100101