jdk/src/jdk.snmp/share/classes/com/sun/jmx/snmp/SnmpStringFixed.java
changeset 27285 1b4ca1c84d23
parent 27284 98699328cef3
parent 27263 819f5f87d485
child 27288 a3f652a37d1e
child 27503 b75a635f1ff3
equal deleted inserted replaced
27284:98699328cef3 27285:1b4ca1c84d23
     1 /*
       
     2  * Copyright (c) 1997, 2007, 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 
       
    27 package com.sun.jmx.snmp;
       
    28 
       
    29 
       
    30 
       
    31 // java imports
       
    32 //
       
    33 import java.lang.Math;
       
    34 
       
    35 /**
       
    36  * Represents an SNMP String defined with a fixed length.
       
    37  * The class is mainly used when dealing with table indexes for which one of the keys
       
    38  * is defined as a <CODE>String</CODE>.
       
    39  *
       
    40  * <p><b>This API is a Sun Microsystems internal API  and is subject
       
    41  * to change without notice.</b></p>
       
    42  */
       
    43 
       
    44 public class SnmpStringFixed extends SnmpString {
       
    45     private static final long serialVersionUID = -9120939046874646063L;
       
    46 
       
    47     // CONSTRUCTORS
       
    48     //-------------
       
    49     /**
       
    50      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified bytes array.
       
    51      * @param v The bytes composing the fixed-string value.
       
    52      */
       
    53     public SnmpStringFixed(byte[] v) {
       
    54         super(v) ;
       
    55     }
       
    56 
       
    57     /**
       
    58      * Constructs a new <CODE>SnmpStringFixed</CODE> with the specified <CODE>Bytes</CODE> array.
       
    59      * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
       
    60      */
       
    61     public SnmpStringFixed(Byte[] v) {
       
    62         super(v) ;
       
    63     }
       
    64 
       
    65     /**
       
    66      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE> value.
       
    67      * @param v The initialization value.
       
    68      */
       
    69     public SnmpStringFixed(String v) {
       
    70         super(v) ;
       
    71     }
       
    72 
       
    73     /**
       
    74      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>bytes</CODE> array
       
    75      * with the specified length.
       
    76      * @param l The length of the fixed-string.
       
    77      * @param v The <CODE>bytes</CODE> composing the fixed-string value.
       
    78      * @exception IllegalArgumentException Either the length or the <CODE>byte</CODE> array is not valid.
       
    79      */
       
    80     public SnmpStringFixed(int l, byte[] v) throws IllegalArgumentException {
       
    81         if ((l <= 0) || (v == null)) {
       
    82             throw new IllegalArgumentException() ;
       
    83         }
       
    84         int length = Math.min(l, v.length);
       
    85         value = new byte[l] ;
       
    86         for (int i = 0 ; i < length ; i++) {
       
    87             value[i] = v[i] ;
       
    88         }
       
    89         for (int i = length ; i < l ; i++) {
       
    90             value[i] = 0 ;
       
    91         }
       
    92     }
       
    93 
       
    94     /**
       
    95      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
       
    96      * with the specified length.
       
    97      * @param l The length of the fixed-string.
       
    98      * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
       
    99      * @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
       
   100      */
       
   101     public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {
       
   102         if ((l <= 0) || (v == null)) {
       
   103             throw new IllegalArgumentException() ;
       
   104         }
       
   105         int length = Math.min(l, v.length);
       
   106         value = new byte[l] ;
       
   107         for (int i = 0 ; i < length ; i++) {
       
   108             value[i] = v[i].byteValue() ;
       
   109         }
       
   110         for (int i = length ; i < l ; i++) {
       
   111             value[i] = 0 ;
       
   112         }
       
   113     }
       
   114 
       
   115     /**
       
   116      * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>
       
   117      * with the specified length.
       
   118      * @param l The length of the fixed-string.
       
   119      * @param s The <CODE>String</CODE> composing the fixed-string value.
       
   120      * @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.
       
   121      */
       
   122     public SnmpStringFixed(int l, String s) throws IllegalArgumentException {
       
   123         if ((l <= 0) || (s == null)) {
       
   124             throw new IllegalArgumentException() ;
       
   125         }
       
   126         byte[] v = s.getBytes();
       
   127         int length = Math.min(l, v.length);
       
   128         value = new byte[l] ;
       
   129         for (int i = 0 ; i < length ; i++) {
       
   130             value[i] = v[i] ;
       
   131         }
       
   132         for (int i = length ; i < l ; i++) {
       
   133             value[i] = 0 ;
       
   134         }
       
   135     }
       
   136 
       
   137     // PUBLIC METHODS
       
   138     //---------------
       
   139     /**
       
   140      * Extracts the fixed-string from an index OID and returns its
       
   141      * value converted as an <CODE>SnmpOid</CODE>.
       
   142      * @param l The number of successive array elements to be retreived
       
   143      * in order to construct the OID.
       
   144      * These elements are retreived starting at the <CODE>start</CODE> position.
       
   145      * @param index The index array.
       
   146      * @param start The position in the index array.
       
   147      * @return The OID representing the fixed-string value.
       
   148      * @exception SnmpStatusException There is no string value
       
   149      * available at the start position.
       
   150      */
       
   151     public static SnmpOid toOid(int l, long[] index, int start) throws SnmpStatusException {
       
   152         try {
       
   153             long[] ids = new long[l] ;
       
   154             for (int i = 0 ; i < l ; i++) {
       
   155                 ids[i] = index[start + i] ;
       
   156             }
       
   157             return new SnmpOid(ids) ;
       
   158         }
       
   159         catch(IndexOutOfBoundsException e) {
       
   160             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
       
   161         }
       
   162     }
       
   163 
       
   164     /**
       
   165      * Scans an index OID, skip the string value and returns the position
       
   166      * of the next value.
       
   167      * @param l The number of successive array elements to be passed
       
   168      * in order to get the position of the next value.
       
   169      * These elements are passed starting at the <CODE>start</CODE> position.
       
   170      * @param index The index array.
       
   171      * @param start The position in the index array.
       
   172      * @return The position of the next value.
       
   173      * @exception SnmpStatusException There is no string value
       
   174      * available at the start position.
       
   175      */
       
   176     public static int nextOid(int l, long[] index, int start) throws SnmpStatusException {
       
   177         int result = start + l ;
       
   178         if (result > index.length) {
       
   179             throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
       
   180         }
       
   181         return result ;
       
   182     }
       
   183 
       
   184     /**
       
   185      * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpStringFixed</CODE> to another OID.
       
   186      * @param l Unused.
       
   187      * @param source An OID representing an <CODE>SnmpStringFixed</CODE> value.
       
   188      * @param dest Where source should be appended.
       
   189      */
       
   190     public static void appendToOid(int l, SnmpOid source, SnmpOid dest) {
       
   191         dest.append(source) ;
       
   192     }
       
   193 }