1 /* 2 * Copyright (c) 2004, 2010, 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 26 package sun.jvmstat.perfdata.monitor; 27 28 import sun.jvmstat.monitor.*; 29 import java.nio.ByteBuffer; 30 import java.nio.charset.Charset; 31 32 /** 33 * Class for monitoring a PerfData String instrument. 34 * 35 * @author Brian Doherty 36 * @since 1.5 37 */ 38 public class PerfStringMonitor extends PerfByteArrayMonitor 39 implements StringMonitor { 40 41 private static Charset defaultCharset = Charset.defaultCharset(); 42 43 /** 44 * Constructor to create a StringMonitor object for the string instrument 45 * represented by the data in the given buffer. 46 * 47 * @param name the name of the string instrument 48 * @param v the variability attribute 49 * @param supported support level indicator 50 * @param bb the buffer containing the string instrument data. 51 */ 52 public PerfStringMonitor(String name, Variability v, boolean supported, 53 ByteBuffer bb) { 54 this(name, v, supported, bb, bb.limit()); 55 } 56 57 /** 58 * Constructor to create a StringMonitor object for the string instrument 59 * represented by the data in the given buffer. 60 * 61 * @param name the name of the string instrument 62 * @param v the variability attribute 63 * @param supported support level indicator 64 * @param bb the buffer containing the string instrument data. 65 * @param maxLength the maximum length of the string data. 66 */ 67 public PerfStringMonitor(String name, Variability v, boolean supported, 68 ByteBuffer bb, int maxLength) { 69 super(name, Units.STRING, v, supported, bb, maxLength); 70 } 71 72 /** 73 * {@inheritDoc} 74 * The object returned contains a String with a copy of the current 75 * value of the StringInstrument. 76 * 77 * @return Object - a copy of the current value of the StringInstrument. 78 * The return value is guaranteed to be of type String. 79 */ 80 public Object getValue() { 81 return stringValue(); 82 } 83 84 /** 85 * Return the current value of the StringInstrument as a String. 86 * 87 * @return String - a copy of the current value of the StringInstrument. 88 */ 89 public String stringValue() { 90 String str = ""; 91 byte[] b = byteArrayValue(); 92 93 // catch null strings 94 if ((b == null) || (b.length <= 1) || (b[0] == (byte)0)) { 95 return str; 96 } 97 98 int i; 99 for (i = 0; i < b.length && b[i] != (byte)0; i++); 100 101 return new String(b, 0, i, defaultCharset); 102 } 103 }