KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > corruptio > CorruptRandomAccessFile


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.corruptio.CorruptRandomAccessFile
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util.corruptio;
23 import org.apache.derby.io.StorageRandomAccessFile;
24 import java.io.IOException JavaDoc;
25 import java.io.File JavaDoc;
26
27
28 /**
29  * This class provides a proxy implementation of the StorageRandomAccess File
30  * interface. It is used by CorruptDiskStorageFactory to instrument the database engine
31  * i/o for testing puproses. How the i/o operation are corrupted is based on the values
32  * set in the instance of the Singleton CorruptibleIo class by the tests.
33  * Methods in this class functon similar to java.io.RandomAccessFile except
34  * when modified to perform the corruptios.
35  *
36  * @author <a HREF="mailto:suresh.thalamati@gmail.com">Suresh Thalamati</a>
37  * @version 1.0
38  * @see java.io.RandomAccessFile
39  * @see StorageRandomAccessFile
40  */

41 public class CorruptRandomAccessFile implements StorageRandomAccessFile
42 {
43
44     private StorageRandomAccessFile realRaf;
45     private CorruptibleIo cbio;
46     private File realFile;
47
48     /**
49      * Construct a CorruptRandomAccessFile
50      *
51      * @param raf The real random access file to which calls are delegated fro
52      * this proxy class.
53      */

54     CorruptRandomAccessFile(StorageRandomAccessFile raf, File realFile)
55     {
56         this.realRaf = raf;
57         cbio = CorruptibleIo.getInstance();
58         this.realFile = realFile;
59     }
60
61     
62     /**
63      * Closes this file.
64      */

65     public void close() throws IOException JavaDoc
66     {
67         realRaf.close();
68     }
69
70     /**
71      * Get the current offset in this file.
72      */

73     public long getFilePointer() throws IOException JavaDoc
74     {
75         return realRaf.getFilePointer();
76     }
77
78     /**
79      * Gets the length of this file.
80      */

81     public long length() throws IOException JavaDoc
82     {
83         return realRaf.length();
84     }
85
86     /**
87      * Set the file pointer.
88      */

89     public void seek(long newFilePointer) throws IOException JavaDoc
90     {
91         realRaf.seek(newFilePointer);
92     }
93
94     /**
95      * Sets the length of this file, either extending or truncating it.
96      */

97     public void setLength(long newLength) throws IOException JavaDoc
98     {
99         realRaf.setLength(newLength);
100     }
101     
102     /**
103      * Force any changes out to the persistent store.
104      */

105     public void sync( boolean metaData) throws IOException JavaDoc
106     {
107         realRaf.sync(metaData);
108     }
109
110
111     /*** Following functions Implement DataInput interfaces ****/
112
113     /**
114      * Reads some bytes from an input stream into the byte array.
115      */

116     public void readFully(byte b[]) throws IOException JavaDoc
117     {
118         realRaf.readFully(b);
119     }
120
121     /**
122      *
123      * Reads the specified number of bytes from an input stream.
124      */

125     public void readFully(byte b[], int off, int len) throws IOException JavaDoc
126     {
127         realRaf.readFully(b , off, len);
128     }
129
130     /**
131      * skip over <code>nBytes</code> bytes of data
132      */

133     public int skipBytes(int nBytes) throws IOException JavaDoc
134     {
135         return realRaf.skipBytes(nBytes);
136     }
137
138     /**
139      * Reads a byte and returns true if the byte is not zero
140      * otherwise false.
141      */

142     public boolean readBoolean() throws IOException JavaDoc
143     {
144         return realRaf.readBoolean();
145     }
146
147     /**
148      * returns one input byte from the stream.
149      */

150     public byte readByte() throws IOException JavaDoc
151     {
152         return realRaf.readByte();
153     }
154
155     /**
156      * Reads one input byte in the unsigned form.
157      */

158     public int readUnsignedByte() throws IOException JavaDoc
159     {
160         return realRaf.readUnsignedByte();
161     }
162
163     /**
164      * returns a short value from the stream.
165      */

166     public short readShort() throws IOException JavaDoc
167     {
168         return realRaf.readShort();
169     }
170
171     /**
172      * returns unsigned short.
173      */

174     public int readUnsignedShort() throws IOException JavaDoc
175     {
176         return realRaf.readUnsignedShort();
177     }
178
179     /**
180      * returns a char value from the stream.
181      */

182     public char readChar() throws IOException JavaDoc
183     {
184         return realRaf.readChar();
185     }
186
187     /**
188      * returns an Int from the stream.
189      */

190     public int readInt() throws IOException JavaDoc
191     {
192         return realRaf.readInt();
193     }
194
195     /**
196      * returns a long from the stream.
197      */

198     public long readLong() throws IOException JavaDoc
199     {
200         return realRaf.readLong();
201     }
202
203     /**
204      * returns a float from the stream.
205      */

206     public float readFloat() throws IOException JavaDoc
207     {
208         return realRaf.readFloat();
209     }
210
211     /**
212      * returns a double from the stream.
213      */

214     public double readDouble() throws IOException JavaDoc
215     {
216         return realRaf.readDouble();
217     }
218
219     /**
220      * returns the next line of text from the input stream.
221      */

222     public String JavaDoc readLine() throws IOException JavaDoc
223     {
224         return realRaf.readLine();
225     }
226
227     /**
228      * returns a string that has been encoded using in the UTF-8 format.
229      */

230     public String JavaDoc readUTF() throws IOException JavaDoc
231     {
232         return realRaf.readUTF();
233     }
234
235
236     /* Proxy Implementation of DataOutput interface */
237
238     /**
239      * Writes an int to the output stream .
240      */

241     public void write(int b) throws IOException JavaDoc
242     {
243         realRaf.write(b);
244     }
245
246     /**
247      * Writes all the bytes in array to the stream.
248      */

249     public void write(byte b[]) throws IOException JavaDoc
250     {
251         realRaf.write(b);
252     }
253
254     /**
255      * Writes specified number bytes from array to the stream.
256      * If the corruption flags are enabled, byte array
257      * is corrupted before doing the real write.
258      */

259     public void write(byte b[], int off, int len) throws IOException JavaDoc
260     {
261         if (cbio.isCorruptibleFile(realFile)){
262             //corrupt the input byte array
263
cbio.corrupt(b , off, len);
264         }
265         realRaf.write(b, off, len);
266     }
267
268     /**
269      * Writes a boolean value to this output stream.
270      */

271     public void writeBoolean(boolean value) throws IOException JavaDoc
272     {
273         realRaf.writeBoolean(value);
274     }
275
276     /**
277      * Writes to the eight low-order bits of ant int.
278      *
279      */

280     public void writeByte(int value) throws IOException JavaDoc
281     {
282         realRaf.writeByte(value);
283     }
284
285     /**
286      * Writes a short value to the output stream
287      */

288     public void writeShort(int value) throws IOException JavaDoc
289     {
290         realRaf.writeShort(value);
291     }
292
293     /**
294      * Writes a char value to the output stream.
295      *
296      * @param value the <code>char</code> value to be written.
297      * @exception IOException if an I/O error occurs.
298      */

299     public void writeChar(int value) throws IOException JavaDoc
300     {
301         realRaf.writeChar(value);
302     }
303
304     /**
305      * Writes an int value to the output stream.
306      */

307     public void writeInt(int value) throws IOException JavaDoc
308     {
309         realRaf.writeInt(value);
310     }
311
312     /**
313      * Writes a long value to the output stream.
314      */

315     public void writeLong(long value) throws IOException JavaDoc
316     {
317         realRaf.writeLong(value);
318     }
319
320     /**
321      * Writes a float value to the output stream.
322      */

323     public void writeFloat(float value) throws IOException JavaDoc
324     {
325         realRaf.writeFloat(value);
326     }
327
328     /**
329      * Writes a a double value to the stream.
330      */

331     public void writeDouble(double value) throws IOException JavaDoc
332     {
333         realRaf.writeDouble(value);
334     }
335
336     /**
337      * Writes a string as bytes to the stream.
338      */

339     public void writeBytes(String JavaDoc str) throws IOException JavaDoc
340     {
341         realRaf.writeBytes(str);
342     }
343
344     /**
345      * Writes the string to the stream.
346      */

347     public void writeChars(String JavaDoc str) throws IOException JavaDoc
348     {
349         realRaf.writeChars(str);
350     }
351
352     /**
353      * Writes the string in the utf format.
354      */

355     public void writeUTF(String JavaDoc str) throws IOException JavaDoc
356     {
357         realRaf.writeUTF(str);
358     }
359
360 }
361
Popular Tags