View Javadoc

1   /***
2    * Copyright 2006 Joseph M. Ferner
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *    
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fernsroth.easyio;
17  
18  import java.io.IOException;
19  
20  /***
21   * 
22   * @author Joseph M. Ferner (Near Infinity Corporation)
23   */
24  public class RandomAccessByteArray implements IRandomAccessSource {
25  
26      /***
27       * the data.
28       */
29      private byte[] data;
30  
31      /***
32       * the location. 
33       */
34      private long loc;
35  
36      /***
37       * constructor.
38       * @param data the data.
39       */
40      public RandomAccessByteArray(byte[] data) {
41          this.data = data;
42          this.loc = 0;
43      }
44  
45      /***
46       * {@inheritDoc}
47       */
48      public void seek(long newLocation) {
49          this.loc = newLocation;
50      }
51  
52      /***
53       * {@inheritDoc}
54       */
55      public long getFilePointer() {
56          return this.loc;
57      }
58  
59      /***
60       * {@inheritDoc}
61       */
62      public void write(int b) {
63          this.data[(int) this.loc++] = (byte) b;
64      }
65  
66      /***
67       * {@inheritDoc}
68       */
69      public void write(byte[] buffer) {
70          write(buffer, 0, buffer.length);
71      }
72  
73      /***
74       * {@inheritDoc}
75       */
76      public void write(byte[] buffer, int start, int length) {
77          for (int i = start; i < start + length; i++) {
78              write(buffer[i]);
79          }
80      }
81  
82      /***
83       * {@inheritDoc}
84       */
85      public int read() {
86          return this.data[(int) this.loc++];
87      }
88  
89      /***
90       * {@inheritDoc}
91       */
92      public int read(byte[] buffer, int off, int len) {
93          for (int i = 0; i < len; i++) {
94              buffer[i + off] = this.data[(int) this.loc++];
95          }
96          return len;
97      }
98  
99      /***
100      * {@inheritDoc}
101      */
102     public int read(byte[] buffer) {
103         return read(buffer, 0, buffer.length);
104     }
105 
106     /***
107      * {@inheritDoc}
108      */
109     public void setLength(long length) throws IOException {
110         // TODO should we resize the array?
111     }
112 
113     /***
114      * {@inheritDoc}
115      */
116     public long length() throws IOException {
117         return this.data.length;
118     }
119 }