Path: blob/master/test/jdk/sun/management/jdp/JdpTestUtil.java
41152 views
/*1* Copyright (c) 2012, 2013, 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*/222324import java.io.UnsupportedEncodingException;25import java.util.Arrays;26import java.util.HashMap;27import java.util.Map;28import java.util.logging.ConsoleHandler;29import java.util.logging.Level;30import java.util.logging.Logger;3132/**33* Utility methods for parsing raw JDP packets.34*35* @author Alex Schenkman36*/37public class JdpTestUtil {3839static final int HEADER_SIZE = 4 + 2; // magic + protocol version4041/**42* Reads two bytes, starting at the given position,43* and converts them into an int.44*45* @param data46* @param pos47* @return48*/49static int decode2ByteInt(byte[] data, int pos) {50return (((data[pos] & 0xFF) << 8) | (data[pos + 1] & 0xFF));51}5253/**54* Reads four bytes, starting at the given position,55* and converts them into an int.56*57* @param data58* @param pos59* @return60*/61static int decode4ByteInt(byte[] data, int pos) {62int result = data[pos + 3] & 0xFF;63result = result | ((data[pos + 2] & 0xFF) << 8);64result = result | ((data[pos + 1] & 0xFF) << 16);65result = result | ((data[pos] & 0xFF) << 24);66return result;67}6869/**70* Reads an entry from the given byte array, starting at the given position.71* This is an internal function used by @see readRawPayload(byte[] rawPayload, int size).72* <p/>73* The format of an entry is:74* 2 bytes with the size of the following string.75* n bytes of characters.76*77* @param data78* @param pos79* @return80* @throws UnsupportedEncodingException81*/82static String decodeEntry(byte[] data, int pos)83throws UnsupportedEncodingException {8485int size = JdpTestUtil.decode2ByteInt(data, pos);86pos = pos + 2;87byte[] raw = Arrays.copyOfRange(data, pos, pos + size);88return new String(raw, "UTF-8");89}9091/**92* Builds a Map with the payload, from the raw data.93*94* @param rawData95* @return96* @throws UnsupportedEncodingException97*/98static Map<String, String> readPayload(byte[] rawData)99throws UnsupportedEncodingException {100101int totalSize = rawData.length;102int payloadSize = totalSize - HEADER_SIZE;103byte[] rawPayload = Arrays.copyOfRange(rawData, HEADER_SIZE, HEADER_SIZE + payloadSize);104Map<String, String> payload = readRawPayload(rawPayload, payloadSize);105return payload;106}107108/**109* Builds a map from the payload's raw data.110* This is an internal function used by @see readPayload(byte[] rawData)111*112* @param rawPayload113* @param size114* @return115* @throws UnsupportedEncodingException116*/117static Map<String, String> readRawPayload(byte[] rawPayload, int size)118throws UnsupportedEncodingException {119120String key, value;121Map<String, String> payload = new HashMap<String, String>();122123for (int pos = 0; pos < size; ) {124key = decodeEntry(rawPayload, pos);125pos = pos + 2 + key.length();126value = decodeEntry(rawPayload, pos);127pos = pos + 2 + value.length();128129payload.put(key, value);130}131return payload;132}133134static void enableConsoleLogging(Logger log, Level level) throws SecurityException {135ConsoleHandler handler = new ConsoleHandler();136handler.setLevel(level);137log.addHandler(handler);138log.setLevel(level);139}140141}142143144