01: /*
02: * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * 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.management.counter.perf;
27:
28: import sun.management.counter.*;
29: import java.nio.ByteBuffer;
30: import java.nio.ReadOnlyBufferException;
31: import java.nio.charset.Charset;
32:
33: public class PerfStringCounter extends PerfByteArrayCounter implements
34: StringCounter {
35:
36: private static Charset defaultCharset = Charset.defaultCharset();
37:
38: PerfStringCounter(String name, Variability v, int flags,
39: ByteBuffer bb) {
40: this (name, v, flags, bb.limit(), bb);
41: }
42:
43: PerfStringCounter(String name, Variability v, int flags,
44: int maxLength, ByteBuffer bb) {
45:
46: super (name, Units.STRING, v, flags, maxLength, bb);
47: }
48:
49: // override isVector and getVectorLength in ByteArrayCounter
50: public boolean isVector() {
51: return false;
52: }
53:
54: public int getVectorLength() {
55: return 0;
56: }
57:
58: public Object getValue() {
59: return stringValue();
60: }
61:
62: public String stringValue() {
63:
64: String str = "";
65: byte[] b = byteArrayValue();
66:
67: if (b == null || b.length <= 1) {
68: return str;
69: }
70:
71: int i;
72: for (i = 0; i < b.length && b[i] != (byte) 0; i++)
73: ;
74:
75: // convert byte array to string, using all bytes up to, but
76: // not including the first null byte.
77: return new String(b, 0, i, defaultCharset);
78: }
79:
80: /**
81: * Serialize as a snapshot object.
82: */
83: protected Object writeReplace()
84: throws java.io.ObjectStreamException {
85: return new StringCounterSnapshot(getName(), getUnits(),
86: getVariability(), getFlags(), stringValue());
87: }
88:
89: }
|