Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/SnmpStringFixed.java
38924 views
/*1* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package com.sun.jmx.snmp;27282930// java imports31//32import java.lang.Math;3334/**35* Represents an SNMP String defined with a fixed length.36* The class is mainly used when dealing with table indexes for which one of the keys37* is defined as a <CODE>String</CODE>.38*39* <p><b>This API is a Sun Microsystems internal API and is subject40* to change without notice.</b></p>41*/4243public class SnmpStringFixed extends SnmpString {44private static final long serialVersionUID = -9120939046874646063L;4546// CONSTRUCTORS47//-------------48/**49* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified bytes array.50* @param v The bytes composing the fixed-string value.51*/52public SnmpStringFixed(byte[] v) {53super(v) ;54}5556/**57* Constructs a new <CODE>SnmpStringFixed</CODE> with the specified <CODE>Bytes</CODE> array.58* @param v The <CODE>Bytes</CODE> composing the fixed-string value.59*/60public SnmpStringFixed(Byte[] v) {61super(v) ;62}6364/**65* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE> value.66* @param v The initialization value.67*/68public SnmpStringFixed(String v) {69super(v) ;70}7172/**73* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>bytes</CODE> array74* with the specified length.75* @param l The length of the fixed-string.76* @param v The <CODE>bytes</CODE> composing the fixed-string value.77* @exception IllegalArgumentException Either the length or the <CODE>byte</CODE> array is not valid.78*/79public SnmpStringFixed(int l, byte[] v) throws IllegalArgumentException {80if ((l <= 0) || (v == null)) {81throw new IllegalArgumentException() ;82}83int length = Math.min(l, v.length);84value = new byte[l] ;85for (int i = 0 ; i < length ; i++) {86value[i] = v[i] ;87}88for (int i = length ; i < l ; i++) {89value[i] = 0 ;90}91}9293/**94* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array95* with the specified length.96* @param l The length of the fixed-string.97* @param v The <CODE>Bytes</CODE> composing the fixed-string value.98* @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.99*/100public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {101if ((l <= 0) || (v == null)) {102throw new IllegalArgumentException() ;103}104int length = Math.min(l, v.length);105value = new byte[l] ;106for (int i = 0 ; i < length ; i++) {107value[i] = v[i].byteValue() ;108}109for (int i = length ; i < l ; i++) {110value[i] = 0 ;111}112}113114/**115* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>116* with the specified length.117* @param l The length of the fixed-string.118* @param s The <CODE>String</CODE> composing the fixed-string value.119* @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.120*/121public SnmpStringFixed(int l, String s) throws IllegalArgumentException {122if ((l <= 0) || (s == null)) {123throw new IllegalArgumentException() ;124}125byte[] v = s.getBytes();126int length = Math.min(l, v.length);127value = new byte[l] ;128for (int i = 0 ; i < length ; i++) {129value[i] = v[i] ;130}131for (int i = length ; i < l ; i++) {132value[i] = 0 ;133}134}135136// PUBLIC METHODS137//---------------138/**139* Extracts the fixed-string from an index OID and returns its140* value converted as an <CODE>SnmpOid</CODE>.141* @param l The number of successive array elements to be retreived142* in order to construct the OID.143* These elements are retreived starting at the <CODE>start</CODE> position.144* @param index The index array.145* @param start The position in the index array.146* @return The OID representing the fixed-string value.147* @exception SnmpStatusException There is no string value148* available at the start position.149*/150public static SnmpOid toOid(int l, long[] index, int start) throws SnmpStatusException {151try {152long[] ids = new long[l] ;153for (int i = 0 ; i < l ; i++) {154ids[i] = index[start + i] ;155}156return new SnmpOid(ids) ;157}158catch(IndexOutOfBoundsException e) {159throw new SnmpStatusException(SnmpStatusException.noSuchName) ;160}161}162163/**164* Scans an index OID, skip the string value and returns the position165* of the next value.166* @param l The number of successive array elements to be passed167* in order to get the position of the next value.168* These elements are passed starting at the <CODE>start</CODE> position.169* @param index The index array.170* @param start The position in the index array.171* @return The position of the next value.172* @exception SnmpStatusException There is no string value173* available at the start position.174*/175public static int nextOid(int l, long[] index, int start) throws SnmpStatusException {176int result = start + l ;177if (result > index.length) {178throw new SnmpStatusException(SnmpStatusException.noSuchName) ;179}180return result ;181}182183/**184* Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpStringFixed</CODE> to another OID.185* @param l Unused.186* @param source An OID representing an <CODE>SnmpStringFixed</CODE> value.187* @param dest Where source should be appended.188*/189public static void appendToOid(int l, SnmpOid source, SnmpOid dest) {190dest.append(source) ;191}192}193194195