KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > metanotion > io > RAIFile


1 /*
2 Copyright (c) 2006, Matthew Estes
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13     * Neither the name of Metanotion Software nor the names of its
14 contributors may be used to endorse or promote products derived from this
15 software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29 package net.metanotion.io;
30
31 import java.io.DataInput JavaDoc;
32 import java.io.DataOutput JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.RandomAccessFile JavaDoc;
37
38 public class RAIFile implements RandomAccessInterface, DataInput JavaDoc, DataOutput JavaDoc {
39     private File JavaDoc f;
40     private RandomAccessFile JavaDoc delegate;
41     private boolean r=false, w=false;
42
43     public RAIFile(RandomAccessFile JavaDoc file) throws FileNotFoundException JavaDoc {
44         this.f = null;
45         this.delegate = file;
46     }
47
48     public RAIFile(File JavaDoc file, boolean read, boolean write) throws FileNotFoundException JavaDoc {
49         this.f = file;
50         this.r = read;
51         this.w = write;
52         String JavaDoc mode = "";
53         if(this.r) { mode += "r"; }
54         if(this.w) { mode += "w"; }
55         this.delegate = new RandomAccessFile JavaDoc(file, mode);
56     }
57
58     public long getFilePointer() throws IOException JavaDoc { return delegate.getFilePointer(); }
59     public long length() throws IOException JavaDoc { return delegate.length(); }
60     public int read() throws IOException JavaDoc { return delegate.read(); }
61     public int read(byte[] b) throws IOException JavaDoc { return delegate.read(b); }
62     public int read(byte[] b, int off, int len) throws IOException JavaDoc { return delegate.read(b,off,len); }
63     public void seek(long pos) throws IOException JavaDoc { delegate.seek(pos); }
64     public void setLength(long newLength) throws IOException JavaDoc { delegate.setLength(newLength); }
65
66     // Closeable Methods
67
// TODO May need to change.
68
public void close() throws IOException JavaDoc { delegate.close(); }
69
70     // DataInput Methods
71
public boolean readBoolean() throws IOException JavaDoc { return delegate.readBoolean(); }
72     public byte readByte() throws IOException JavaDoc { return delegate.readByte(); }
73     public char readChar() throws IOException JavaDoc { return delegate.readChar(); }
74     public double readDouble() throws IOException JavaDoc { return delegate.readDouble(); }
75     public float readFloat() throws IOException JavaDoc { return delegate.readFloat(); }
76     public void readFully(byte[] b) throws IOException JavaDoc { delegate.readFully(b); }
77     public void readFully(byte[] b, int off, int len) throws IOException JavaDoc { delegate.readFully(b,off,len); }
78     public int readInt() throws IOException JavaDoc { return delegate.readInt(); }
79     public String JavaDoc readLine() throws IOException JavaDoc { return delegate.readLine(); }
80     public long readLong() throws IOException JavaDoc { return delegate.readLong(); }
81     public short readShort() throws IOException JavaDoc { return delegate.readShort(); }
82     public int readUnsignedByte() throws IOException JavaDoc { return delegate.readUnsignedByte(); }
83     public int readUnsignedShort() throws IOException JavaDoc { return delegate.readUnsignedShort(); }
84
85     /** Read a UTF encoded string
86         I would delegate here. But Java's read/writeUTF combo suck.
87         A signed 2 byte length is not enough.
88         This reads a 4 byte length.
89         The upper byte MUST be zero, if its not, then its not this method and has used an
90         extensible length encoding.
91         This is followed by the bytes of the UTF encoded string, as
92         returned by String.getBytes("UTF-8");
93     */

94     public String JavaDoc readUTF() throws IOException JavaDoc {
95         int len = delegate.readInt();
96         if((len < 0) || (len >= 16777216)) { throw new IOException JavaDoc("Bad Length Encoding"); }
97         byte[] bytes = new byte[len];
98         int l = delegate.read(bytes);
99         if(l==-1) { throw new IOException JavaDoc("EOF while reading String"); }
100         String JavaDoc s = new String JavaDoc(bytes, "UTF-8");
101         return s;
102     }
103
104     public int skipBytes(int n) throws IOException JavaDoc { return delegate.skipBytes(n); }
105
106     // DataOutput Methods
107
public void write(int b) throws IOException JavaDoc { delegate.write(b); }
108     public void write(byte[] b) throws IOException JavaDoc { delegate.write(b); }
109     public void write(byte[] b, int off, int len) throws IOException JavaDoc { delegate.write(b,off,len); }
110     public void writeBoolean(boolean v) throws IOException JavaDoc { delegate.writeBoolean(v); }
111     public void writeByte(int v) throws IOException JavaDoc { delegate.writeByte(v); }
112     public void writeShort(int v) throws IOException JavaDoc { delegate.writeShort(v); }
113     public void writeChar(int v) throws IOException JavaDoc { delegate.writeChar(v); }
114     public void writeInt(int v) throws IOException JavaDoc { delegate.writeInt(v); }
115     public void writeLong(long v) throws IOException JavaDoc { delegate.writeLong(v); }
116     public void writeFloat(float v) throws IOException JavaDoc { delegate.writeFloat(v); }
117     public void writeDouble(double v) throws IOException JavaDoc { delegate.writeDouble(v); }
118     public void writeBytes(String JavaDoc s) throws IOException JavaDoc { delegate.writeBytes(s); }
119     public void writeChars(String JavaDoc s) throws IOException JavaDoc { delegate.writeChars(s); }
120
121     /** Write a UTF encoded string
122         I would delegate here. But Java's read/writeUTF combo suck.
123         A signed 2 byte length is not enough.
124         This writes a 4 byte length.
125         The upper byte MUST be zero, if its not, then its not this method and has used an
126         extensible length encoding.
127         This is followed by the bytes of the UTF encoded string, as
128         returned by String.getBytes("UTF-8");
129     */

130     public void writeUTF(String JavaDoc str) throws IOException JavaDoc {
131         byte[] string = str.getBytes("UTF-8");
132         if(string.length >= 16777216) { throw new IOException JavaDoc("String to long for encoding type"); }
133         delegate.writeInt(string.length);
134         delegate.write(string);
135     }
136 }
137
Popular Tags