001: /*_############################################################################
002: _##
003: _## SNMP4J - ByteArrayWindow.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.security;
022:
023: /**
024: * The <code>ByteArrayWindow</code> provides windowed access to a subarray
025: * of a byte array.
026: * @author Frank Fock
027: * @version 1.0
028: */
029: public class ByteArrayWindow {
030:
031: private byte[] value;
032: private int offset;
033: private int length;
034:
035: /**
036: * Creates a byte array window that provides access to the bytes in the
037: * supplied array between the position starting at the supplied offset.
038: * @param value
039: * the underlying byte array.
040: * @param offset
041: * the starting position of the created window.
042: * @param length
043: * the length of the window.
044: */
045: public ByteArrayWindow(byte[] value, int offset, int length) {
046: this .value = value;
047: this .offset = offset;
048: this .length = length;
049: }
050:
051: public byte[] getValue() {
052: return value;
053: }
054:
055: public void setValue(byte[] value) {
056: this .value = value;
057: }
058:
059: public int getOffset() {
060: return offset;
061: }
062:
063: public void set(int i, byte b) {
064: if (i >= length) {
065: throw new IndexOutOfBoundsException("" + i + " >= "
066: + length);
067: }
068: if (i < 0) {
069: throw new IndexOutOfBoundsException("" + i);
070: }
071: this .value[i + offset] = b;
072: }
073:
074: public byte get(int i) {
075: if (i >= length) {
076: throw new IndexOutOfBoundsException("" + i + " >= "
077: + length);
078: }
079: if (i < 0) {
080: throw new IndexOutOfBoundsException("" + i);
081: }
082: return value[i + offset];
083: }
084:
085: public int getLength() {
086: return length;
087: }
088:
089: /**
090: * Indicates whether some other object is "equal to" this one.
091: *
092: * @param obj the reference object with which to compare.
093: * @return <code>true</code> if this object is the same as the obj argument;
094: * <code>false</code> otherwise.
095: */
096: public boolean equals(Object obj) {
097: if (obj instanceof ByteArrayWindow) {
098: ByteArrayWindow other = (ByteArrayWindow) obj;
099: if (other.length != length) {
100: return false;
101: }
102: for (int i = 0; i < length; i++) {
103: if (other.value[i] != value[i]) {
104: return false;
105: }
106: }
107: return true;
108: }
109: return false;
110: }
111:
112: public boolean equals(ByteArrayWindow other, int maxBytesToCompare) {
113: if ((other.length < maxBytesToCompare)
114: || (length < maxBytesToCompare)) {
115: return false;
116: }
117: for (int i = 0; i < maxBytesToCompare; i++) {
118: if (value[offset + i] != other.value[other.offset + i]) {
119: return false;
120: }
121: }
122: return true;
123: }
124: }
|