Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/io/util/ByteArray.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  
19  package org.activemq.io.util;
20  
21  /**
22   * Simple holder for a an array of Bytes - used instead of a ByteBuffer to avoid unecessary System.array() copies
23   * 
24   * @version $Revision: 1.1.1.1 $
25   */
26  public class ByteArray {
27      private byte[] buf;
28      private int offset;
29      private int length;
30  
31      /**
32       * Construct an empty ByteArray
33       */
34      public ByteArray() {
35      }
36  
37      /**
38       * Create a byte array
39       * 
40       * @param buf
41       */
42      public ByteArray(byte[] buf) {
43          this(buf, 0, buf.length);
44      }
45  
46      /**
47       * Create a ByteArray
48       * 
49       * @param buf
50       * @param offset
51       * @param length
52       */
53      public ByteArray(byte[] buf, int offset, int length) {
54          this.buf = buf;
55          this.offset = offset;
56          this.length = length;
57      }
58  
59      /**
60       * clear the values held by this ByteArray
61       */
62      public void clear() {
63          buf = null;
64          offset = 0;
65          length = 0;
66      }
67  
68      /**
69       * reset values
70       * 
71       * @param buf
72       */
73      public void reset(byte[] buf) {
74          if (buf != null) {
75              reset(buf, 0, buf.length);
76          }
77          else {
78              clear();
79          }
80      }
81  
82      /**
83       * reset values
84       * 
85       * @param buf
86       * @param offset
87       * @param length
88       */
89      public void reset(byte[] buf, int offset, int length) {
90          this.buf = buf;
91          this.offset = offset;
92          this.length = length;
93      }
94  
95      /**
96       * @return Returns the buf.
97       */
98      public byte[] getBuf() {
99          return buf;
100     }
101 
102     /**
103      * @param buf The buf to set.
104      */
105     public void setBuf(byte[] buf) {
106         this.buf = buf;
107     }
108 
109     /**
110      * @return Returns the length.
111      */
112     public int getLength() {
113         return length;
114     }
115 
116     /**
117      * @param length The length to set.
118      */
119     public void setLength(int length) {
120         this.length = length;
121     }
122 
123     /**
124      * @return Returns the offset.
125      */
126     public int getOffset() {
127         return offset;
128     }
129 
130     /**
131      * @param offset The offset to set.
132      */
133     public void setOffset(int offset) {
134         this.offset = offset;
135     }
136     
137     /**
138      * return the byte at the position
139      * @param position
140      * @return
141      */
142     public byte get(int position){
143         return buf[offset + position];
144     }
145 
146     /**
147      * make a copy
148      * 
149      * @return a copy of it's self
150      */
151     public ByteArray copy() {
152         ByteArray result = new ByteArray();
153         if (buf != null) {
154             byte[] data = new byte[length];
155             System.arraycopy(buf, offset, data, 0, length);
156             result.reset(data);
157         }
158         return result;
159     }
160 }