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 
  31 /**
  32  * Class for monitoring a PerfData Byte Array instrumentation object.
  33  *
  34  * This class is provided to support the PerfStringMonitor classes.
  35  * Instrumentation objects of this direct type currently cannot be
  36  * created or monitored.
  37  *
  38  * @author Brian Doherty
  39  * @since 1.5
  40  * @see sun.jvmstat.instrument.ByteArrayInstrument
  41  */
  42 public class PerfByteArrayMonitor extends AbstractMonitor
  43        implements ByteArrayMonitor {
  44 
  45     /**
  46      * The buffer containing the data for the byte array instrument.
  47      */
  48     ByteBuffer bb;
  49 
  50     /**
  51      * Constructor to create a ByteArrayMonitor for the byte array instrument
  52      * represented by the data in the given buffer.
  53      *
  54      * @param name the name of the instrumentation object
  55      * @param u the units of measure attribute
  56      * @param v the variability attribute
  57      * @param supported support level indicator
  58      * @param bb the buffer containing the byte array instrument data
  59      * @param vectorLength the length of the vector.
  60      */
  61     public PerfByteArrayMonitor(String name, Units u, Variability v,
  62                                 boolean supported, ByteBuffer bb,
  63                                 int vectorLength) {
  64         super(name, u, v, supported, vectorLength);
  65         this.bb = bb;
  66     }
  67 
  68     /**
  69      * {@inheritDoc}
  70      * The object returned contains a byte[] with a copy of the current
  71      * elements of the ByteArrayInstrument.
  72      *
  73      * @return Object - a copy of the current value of the elements of the
  74      *                  byte array instrument. The return type is guaranteed
  75      *                  to be of type byte[].
  76      */
  77     public Object getValue() {
  78         return byteArrayValue();
  79     }
  80 
  81     /**
  82      * Get a copy of the elements of the byte array instrument.
  83      *
  84      * @return byte[] - a copy of the current value of the elements of the
  85      *                  byte array instrument.
  86      */
  87     public byte[] byteArrayValue() {
  88         bb.position(0);
  89         byte[] b = new byte[bb.limit()];
  90 
  91         // copy the bytes
  92         bb.get(b);
  93 
  94         return b;
  95     }
  96 
  97     /**
  98      * Get the current value of an element of the byte array instrument.
  99      *
 100      * @return byte - a copy of the current value of the element at index
 101      *                <tt>index</tt> of the byte array instrument.
 102      */
 103     public byte byteAt(int index) {
 104         bb.position(index);
 105         return bb.get();
 106     }
 107 
 108     /**
 109      * Get the maximum length of the byte array for this byte array instrument.
 110      *
 111      * @return int - the maximum length of the byte array.
 112      */
 113     public int getMaximumLength() {
 114         return bb.limit();
 115     }
 116 }