KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > security > ByteArrayWindow


1 /*_############################################################################
2   _##
3   _## SNMP4J - ByteArrayWindow.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.security;
22
23 /**
24  * The <code>ByteArrayWindow</code> provides windowed access to a subarray
25  * of a byte array.
26  * @author Frank Fock
27  * @version 1.0
28  */

29 public class ByteArrayWindow {
30
31   private byte[] value;
32   private int offset;
33   private int length;
34
35   /**
36    * Creates a byte array window that provides access to the bytes in the
37    * supplied array between the position starting at the supplied offset.
38    * @param value
39    * the underlying byte array.
40    * @param offset
41    * the starting position of the created window.
42    * @param length
43    * the length of the window.
44    */

45   public ByteArrayWindow(byte[] value, int offset, int length) {
46     this.value = value;
47     this.offset = offset;
48     this.length = length;
49   }
50
51   public byte[] getValue() {
52     return value;
53   }
54
55   public void setValue(byte[] value) {
56     this.value = value;
57   }
58
59   public int getOffset() {
60     return offset;
61   }
62
63   public void set(int i, byte b) {
64     if (i >= length) {
65       throw new IndexOutOfBoundsException JavaDoc("" + i + " >= " + length);
66     }
67     if (i < 0) {
68       throw new IndexOutOfBoundsException JavaDoc("" + i);
69     }
70     this.value[i+offset] = b;
71   }
72
73   public byte get(int i) {
74     if (i >= length) {
75       throw new IndexOutOfBoundsException JavaDoc("" + i + " >= " + length);
76     }
77     if (i < 0) {
78       throw new IndexOutOfBoundsException JavaDoc("" + i);
79     }
80     return value[i+offset];
81   }
82
83   public int getLength() {
84     return length;
85   }
86
87   /**
88    * Indicates whether some other object is "equal to" this one.
89    *
90    * @param obj the reference object with which to compare.
91    * @return <code>true</code> if this object is the same as the obj argument;
92    * <code>false</code> otherwise.
93    */

94   public boolean equals(Object JavaDoc obj) {
95     if (obj instanceof ByteArrayWindow) {
96       ByteArrayWindow other = (ByteArrayWindow) obj;
97       if (other.length != length) {
98         return false;
99       }
100       for (int i=0; i<length; i++) {
101         if (other.value[i] != value[i]) {
102           return false;
103         }
104       }
105       return true;
106     }
107     return false;
108   }
109
110   public boolean equals(ByteArrayWindow other, int maxBytesToCompare) {
111     if ((other.length < maxBytesToCompare) ||
112         (length < maxBytesToCompare)) {
113       return false;
114     }
115     for (int i=0; i<maxBytesToCompare; i++) {
116       if (value[offset+i] != other.value[other.offset+i]) {
117         return false;
118       }
119     }
120     return true;
121   }
122 }
123
Popular Tags