001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Nam Nguyen
028: */
029:
030: package com.caucho.server.snmp.types;
031:
032: /*
033: * SNMP ObjectIdentifier type.
034: */
035: public class ObjectIdentifierValue extends SnmpValue {
036: protected String _id;
037:
038: /*
039: * @param id is in dot notation (i.e. "1.2.3.4.5")
040: */
041: public ObjectIdentifierValue(String id) {
042: _id = id;
043: }
044:
045: @Override
046: public int getType() {
047: return SnmpValue.OBJECT_IDENTIFIER;
048: }
049:
050: @Override
051: public String getString() {
052: return _id;
053: }
054:
055: @Override
056: public void toAsn1(StringBuilder sb) {
057: StringBuilder innerSb = new StringBuilder();
058:
059: int x = 0;
060:
061: char ch;
062: int i = 0;
063: while (i < _id.length() && (ch = _id.charAt(i++)) != '.') {
064: x = x * 10 + ch - '0';
065: }
066:
067: int y = 0;
068:
069: while (i < _id.length() && (ch = _id.charAt(i++)) != '.') {
070: y = y * 10 + ch - '0';
071: }
072:
073: encodeInteger(innerSb, x * 40 + y);
074:
075: while (i < _id.length()) {
076: int val = 0;
077:
078: while (i < _id.length() && (ch = _id.charAt(i++)) != '.') {
079: val = val * 10 + ch - '0';
080: }
081:
082: encodeInteger(innerSb, val);
083: }
084:
085: header(sb, innerSb.length());
086:
087: sb.append(innerSb);
088: }
089:
090: private void encodeInteger(StringBuilder sb, int val) {
091: int len = 1;
092:
093: int total = val;
094: while ((total >>= 7) > 0) {
095: len++;
096: }
097:
098: for (int i = 1; i < len; i++) {
099: int shift = (len - i) * 7;
100: sb.append((char) ((val >> shift) & 0x7F | 0x80));
101: }
102:
103: sb.append((char) (val & 0x7F));
104: }
105:
106: public String toString() {
107: return "ObjectIdentifier[" + _id + "]";
108: }
109:
110: }
|