PerfStringMonitor.java revision 16638:0eb0f644345d
1184610Salfred/*
2184610Salfred * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3184610Salfred * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4184610Salfred *
5184610Salfred * This code is free software; you can redistribute it and/or modify it
6184610Salfred * under the terms of the GNU General Public License version 2 only, as
7184610Salfred * published by the Free Software Foundation.  Oracle designates this
8184610Salfred * particular file as subject to the "Classpath" exception as provided
9184610Salfred * by Oracle in the LICENSE file that accompanied this code.
10184610Salfred *
11184610Salfred * This code is distributed in the hope that it will be useful, but WITHOUT
12184610Salfred * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13184610Salfred * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14184610Salfred * version 2 for more details (a copy is included in the LICENSE file that
15184610Salfred * accompanied this code).
16184610Salfred *
17184610Salfred * You should have received a copy of the GNU General Public License version
18184610Salfred * 2 along with this work; if not, write to the Free Software Foundation,
19184610Salfred * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20184610Salfred *
21184610Salfred * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22184610Salfred * or visit www.oracle.com if you need additional information or have any
23184610Salfred * questions.
24184610Salfred */
25184610Salfred
26184610Salfredpackage sun.jvmstat.perfdata.monitor;
27184610Salfred
28184610Salfredimport sun.jvmstat.monitor.*;
29184610Salfredimport java.nio.ByteBuffer;
30184610Salfredimport java.nio.charset.Charset;
31184610Salfred
32184610Salfred/**
33184610Salfred * Class for monitoring a PerfData String instrument.
34184610Salfred *
35184610Salfred * @author Brian Doherty
36184610Salfred * @since 1.5
37184610Salfred */
38184610Salfredpublic class PerfStringMonitor extends PerfByteArrayMonitor
39184610Salfred       implements StringMonitor {
40184610Salfred
41184610Salfred    private static Charset defaultCharset = Charset.defaultCharset();
42184610Salfred
43184610Salfred    /**
44184610Salfred     * Constructor to create a StringMonitor object for the string instrument
45184610Salfred     * represented by the data in the given buffer.
46184610Salfred     *
47184610Salfred     * @param name the name of the string instrument
48184610Salfred     * @param v the variability attribute
49184610Salfred     * @param supported support level indicator
50184610Salfred     * @param bb the buffer containing the string instrument data.
51184610Salfred     */
52184610Salfred    public PerfStringMonitor(String name, Variability v, boolean supported,
53184610Salfred                             ByteBuffer bb) {
54184610Salfred        this(name, v, supported, bb, bb.limit());
55184610Salfred    }
56184610Salfred
57184610Salfred    /**
58184610Salfred     * Constructor to create a StringMonitor object for the string instrument
59184610Salfred     * represented by the data in the given buffer.
60184610Salfred     *
61184610Salfred     * @param name the name of the string instrument
62184610Salfred     * @param v the variability attribute
63194677Sthompsa     * @param supported support level indicator
64194677Sthompsa     * @param bb the buffer containing the string instrument data.
65194677Sthompsa     * @param maxLength the maximum length of the string data.
66194677Sthompsa     */
67194677Sthompsa    public PerfStringMonitor(String name, Variability v, boolean supported,
68194677Sthompsa                             ByteBuffer bb, int maxLength) {
69194677Sthompsa        super(name, Units.STRING, v, supported, bb, maxLength);
70194677Sthompsa    }
71194677Sthompsa
72194677Sthompsa    /**
73194677Sthompsa     * {@inheritDoc}
74194677Sthompsa     * The object returned contains a String with a copy of the current
75194677Sthompsa     * value of the StringInstrument.
76194677Sthompsa     *
77194677Sthompsa     * @return Object - a copy of the current value of the StringInstrument.
78194677Sthompsa     *                  The return value is guaranteed to be of type String.
79194677Sthompsa     */
80194677Sthompsa    public Object getValue() {
81197570Sthompsa        return stringValue();
82197570Sthompsa    }
83194677Sthompsa
84294637Sian    /**
85294637Sian     * Return the current value of the StringInstrument as a String.
86188942Sthompsa     *
87194677Sthompsa     * @return String - a copy of the current value of the StringInstrument.
88194677Sthompsa     */
89184610Salfred    public String stringValue() {
90194228Sthompsa        String str = "";
91188942Sthompsa        byte[] b = byteArrayValue();
92194677Sthompsa
93188942Sthompsa        // catch null strings
94184610Salfred        if ((b == null) || (b.length <= 1) || (b[0] == (byte)0)) {
95188942Sthompsa            return str;
96184610Salfred        }
97197570Sthompsa
98197570Sthompsa        int i;
99227309Sed        for (i = 0; i < b.length && b[i] != (byte)0; i++);
100197570Sthompsa
101283341Sian        return new String(b, 0, i, defaultCharset);
102283341Sian    }
103283341Sian}
104294637Sian