KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > OctetString


1 package com.quadcap.util;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.OutputStream JavaDoc;
45
46 /**
47  * An OctetString is a variable length string of octets.
48  *
49  * @author Stan Bailes
50  */

51 public class OctetString {
52     byte[] data;
53
54     /**
55      * Construct a new OctetString using the specified byte array. Note
56      * that we simply acquire the byte array -- no copying is done!
57      *
58      * @param data the byte array making up the string
59      */

60     public OctetString(byte[] data) {
61     this.data = data;
62     }
63
64     /**
65      * Construct a new OctetString using a substring from the specified byte
66      * array. This constructor <b>does</b> allocate a new byte array and
67      * copies data from the original array.
68      *
69      * @param data the source byte array
70      * @param offset the position of the first character in the substring
71      * @param len the number of bytes in the new string
72      */

73     public OctetString(byte[] data, int offset, int len) {
74     this.data = new byte[len];
75     System.arraycopy(data, offset, this.data, 0, len);
76     }
77
78     /**
79      * Construct a new OctetString from the specified java.lang.String value.
80      *
81      * @param str the string value
82      */

83     public OctetString(String JavaDoc str) {
84     this.data = str.getBytes();
85     }
86
87     /**
88      * Return the string representation of this item.
89      *
90      * @return the string value
91      */

92     public String JavaDoc toString() { return new String JavaDoc(data); }
93
94     /**
95      * For types which can be directly handled by comparators, return this
96      * object as a byte array. For special types (e.g., NIL or List),
97      * return null.
98      */

99     public byte[] getBytes() { return data; }
100
101     public int hashCode() {
102     return new String JavaDoc(data).hashCode();
103     }
104     
105     public boolean equals(Object JavaDoc obj) {
106     if (obj instanceof OctetString) {
107         return OctetComparator.cmp.compare(this, (OctetString)obj) == 0;
108     }
109     return false;
110     }
111
112     public boolean equalsIgnoreCase(Object JavaDoc obj) {
113     if (obj instanceof OctetString) {
114             OctetString other = (OctetString)obj;
115         return OctetComparator.casecmp.compare(this, other) == 0;
116     }
117     return false;
118     }
119
120     /**
121      * Return the size of this string.
122      */

123     public int size() { return data.length; }
124
125     /**
126      * String comparison using the binary equality comparator
127      */

128     public static boolean equals(OctetString s1, int pos1,
129                  OctetString s2, int pos2, int len) {
130     for (int i = 0; i < len; i++) {
131         if (pos1 + i >= s1.data.length ||
132         pos2 + i >= s2.data.length) return false;
133         if (s1.data[pos1 + i] != s2.data[pos2 + i]) return false;
134     }
135     return true;
136     }
137
138     /**
139      * String search for character. Should this use a comparator? XXX
140      *
141      * @param c the character to search for
142      * @return the index of the first occurrence of the character in this
143      * string, or -1 if the character does not occur in the string.
144      */

145     public int indexOf(char c) {
146     byte b = (byte)c;
147     for (int i = 0; i < data.length; i++) if (data[i] == b) return i;
148     return -1;
149     }
150
151     /**
152      * String search for character. Should this use a comparator? XXX
153      *
154      * @param c the character to search for
155      * @return the index of the last occurrence of the character in this
156      * string, or -1 if the character does not occur in the string.
157      */

158     public int lastIndexOf(char c) {
159     byte b = (byte)c;
160     for (int i = data.length-1; i >= 0; i--) if (data[i] == b) return i;
161     return -1;
162     }
163
164     /**
165      * Return a new string containing the specified substring of this string.
166      *
167      * @param start the position of the first character of the substring
168      * @param end the position of the character after the substring
169      * @return the substring
170      */

171     public OctetString substring(int start, int end) {
172     return new OctetString(data, start, end - start);
173     }
174
175     /**
176      * Return a new string containing the specified substring of this string,
177      * starting with the specified character, and ending with the last
178      * character of the string.
179      *
180      * @param start the position of the first character of the substring
181      * @return the substring
182      */

183     public OctetString substring(int start) {
184     return substring(start, size());
185     }
186     
187
188     /**
189      * A truly general purpose function designed for a specified task,
190      * this returns a new string containing the portion of this string
191      * after the last occurrence of the specified character.
192      *
193      * <p>For example:<pre>
194      * "/addressbook/user/stan/ABC457".afterLast('/') returns "ABC457"
195      * </pre>
196      *
197      * @param c the character
198      * @return a new <code>OctetString</code> containing the required suffix
199      */

200     public OctetString afterLast(char c) {
201     int pos = data.length;
202     while (pos >= 0 && c != data[--pos]) continue;
203     pos++;
204     int len = data.length - pos;
205     return substring(pos);
206     }
207
208     /**
209      * Return substring containing the portion of this string before the
210      * first occurence of the specified character.
211      *
212      * @param c the character
213      * @return a new <code>OctetString</code> containing the substring.
214      */

215     public OctetString before(char c) {
216     int idx = indexOf(c);
217     if (idx < 0) return null;
218     return substring(0, idx);
219     }
220
221     /**
222      * A truly general purpose function designed for a specified task,
223      * this returns a new string containing the portion of this string
224      * before the last occurrence of the specified character.
225      *
226      * <p>For example:<pre>
227      * "/addressbook/user/stan/ABC457".beforeLast('/') returns
228      * "/addressbook/user/stan"
229      * </pre>
230      *
231      * @param c the character
232      * @return a new <code>OctetString</code> containing the required prefix
233      */

234     public OctetString beforeLast(char c) {
235     int len = data.length;
236     while (len >= 0 && c != data[--len]) continue;
237     return substring(0, len);
238     }
239
240     /**
241      * Another general purpose routine :-)
242      * This one converts a particular substring of this string to an
243      * integer.
244      *
245      * @param start the position of the first character of the substring
246      * @param end the position of the character after the substring
247      *
248      */

249     public int getIntSubString(int start, int end) {
250     int val = 0;
251         for (int i = 0; i < end; i++) {
252             val *= 10;
253             val += Character.digit((char)(data[i]), 10);
254         }
255     return val;
256     }
257
258     /**
259      * String concatenation
260      */

261     public OctetString append(String JavaDoc s) {
262     return append(s.getBytes());
263     }
264      
265     /**
266      * String concatenation
267      */

268     public OctetString append(OctetString s) {
269     return append(s.data);
270     }
271      
272     /**
273      * String concatenation
274      */

275     public OctetString append(byte[] b) {
276     int len = data.length + b.length;
277     byte[] ret = new byte[len];
278     System.arraycopy(data, 0, ret, 0, data.length);
279     System.arraycopy(b, 0, ret, data.length, b.length);
280     return new OctetString(ret);
281     }
282      
283 }
284
285
Popular Tags