J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.jvmstat.perfdata.monitor; |
| 27 | |
| 28 | import sun.jvmstat.monitor.*; |
| 29 | import sun.management.counter.Units; |
| 30 | import sun.management.counter.Variability; |
| 31 | import java.nio.ByteBuffer; |
| 32 | import java.nio.charset.Charset; |
| 33 | |
| 34 | /** |
| 35 | * Class for monitoring a PerfData String instrument. |
| 36 | * |
| 37 | * @author Brian Doherty |
| 38 | * @since 1.5 |
| 39 | */ |
| 40 | public class PerfStringMonitor extends PerfByteArrayMonitor |
| 41 | implements StringMonitor { |
| 42 | |
| 43 | private static Charset defaultCharset = Charset.defaultCharset(); |
| 44 | |
| 45 | /** |
| 46 | * Constructor to create a StringMonitor object for the string instrument |
| 47 | * represented by the data in the given buffer. |
| 48 | * |
| 49 | * @param name the name of the string instrument |
| 50 | * @param v the variability attribute |
| 51 | * @param supported support level indicator |
| 52 | * @param bb the buffer containing the string instrument data. |
| 53 | */ |
| 54 | public PerfStringMonitor(String name, Variability v, boolean supported, |
| 55 | ByteBuffer bb) { |
| 56 | this(name, v, supported, bb, bb.limit()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Constructor to create a StringMonitor object for the string instrument |
| 61 | * represented by the data in the given buffer. |
| 62 | * |
| 63 | * @param name the name of the string instrument |
| 64 | * @param v the variability attribute |
| 65 | * @param supported support level indicator |
| 66 | * @param bb the buffer containing the string instrument data. |
| 67 | * @param maxLength the maximum length of the string data. |
| 68 | */ |
| 69 | public PerfStringMonitor(String name, Variability v, boolean supported, |
| 70 | ByteBuffer bb, int maxLength) { |
| 71 | super(name, Units.STRING, v, supported, bb, maxLength); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * {@inheritDoc} |
| 76 | * The object returned contains a String with a copy of the current |
| 77 | * value of the StringInstrument. |
| 78 | * |
| 79 | * @return Object - a copy of the current value of the StringInstrument. |
| 80 | * The return value is guaranteed to be of type String. |
| 81 | */ |
| 82 | public Object getValue() { |
| 83 | return stringValue(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return the current value of the StringInstrument as a String. |
| 88 | * |
| 89 | * @return String - a copy of the current value of the StringInstrument. |
| 90 | */ |
| 91 | public String stringValue() { |
| 92 | String str = ""; |
| 93 | byte[] b = byteArrayValue(); |
| 94 | |
| 95 | // catch null strings |
| 96 | if ((b == null) || (b.length <= 1) || (b[0] == (byte)0)) { |
| 97 | return str; |
| 98 | } |
| 99 | |
| 100 | int i; |
| 101 | for (i = 0; i < b.length && b[i] != (byte)0; i++); |
| 102 | |
| 103 | return new String(b, 0, i, defaultCharset); |
| 104 | } |
| 105 | } |