001: /*
002: * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.jvmstat.perfdata.monitor;
027:
028: import sun.jvmstat.monitor.*;
029: import sun.management.counter.Units;
030: import sun.management.counter.Variability;
031: import java.nio.ByteBuffer;
032: import java.nio.charset.Charset;
033:
034: /**
035: * Class for monitoring a PerfData String instrument.
036: *
037: * @author Brian Doherty
038: * @version 1.9, 05/05/07
039: * @since 1.5
040: */
041: public class PerfStringMonitor extends PerfByteArrayMonitor implements
042: StringMonitor {
043:
044: private static Charset defaultCharset = Charset.defaultCharset();
045:
046: /**
047: * Constructor to create a StringMonitor object for the string instrument
048: * represented by the data in the given buffer.
049: *
050: * @param name the name of the string instrument
051: * @param v the variability attribute
052: * @param supported support level indicator
053: * @param bb the buffer containing the string instrument data.
054: */
055: public PerfStringMonitor(String name, Variability v,
056: boolean supported, ByteBuffer bb) {
057: this (name, v, supported, bb, bb.limit());
058: }
059:
060: /**
061: * Constructor to create a StringMonitor object for the string instrument
062: * represented by the data in the given buffer.
063: *
064: * @param name the name of the string instrument
065: * @param v the variability attribute
066: * @param supported support level indicator
067: * @param bb the buffer containing the string instrument data.
068: * @param maxLength the maximum length of the string data.
069: */
070: public PerfStringMonitor(String name, Variability v,
071: boolean supported, ByteBuffer bb, int maxLength) {
072: super (name, Units.STRING, v, supported, bb, maxLength);
073: }
074:
075: /**
076: * {@inheritDoc}
077: * The object returned contains a String with a copy of the current
078: * value of the StringInstrument.
079: *
080: * @return Object - a copy of the current value of the StringInstrument.
081: * The return value is guaranteed to be of type String.
082: */
083: public Object getValue() {
084: return stringValue();
085: }
086:
087: /**
088: * Return the current value of the StringInstrument as a String.
089: *
090: * @return String - a copy of the current value of the StringInstrument.
091: */
092: public String stringValue() {
093: String str = "";
094: byte[] b = byteArrayValue();
095:
096: // catch null strings
097: if ((b == null) || (b.length <= 1) || (b[0] == (byte) 0)) {
098: return str;
099: }
100:
101: int i;
102: for (i = 0; i < b.length && b[i] != (byte) 0; i++)
103: ;
104:
105: return new String(b, 0, i, defaultCharset);
106: }
107: }
|