KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > rof > ChannelReadOnlyFile


1 /*
2  * ChannelReadOnlyFile.java
3  *
4  * Created on 17. Dezember 2005, 18:36
5  */

6 /*
7  * Copyright 2005 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io.rof;
23
24 import java.io.EOFException JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.RandomAccessFile JavaDoc;
29 import java.nio.BufferUnderflowException JavaDoc;
30 import java.nio.ByteBuffer JavaDoc;
31 import java.nio.MappedByteBuffer JavaDoc;
32 import java.nio.channels.FileChannel JavaDoc;
33
34 /**
35  * A {@link ReadOnlyFile} using channels.
36  *
37  * @author Christian Schlichtherle
38  */

39 public class ChannelReadOnlyFile implements ReadOnlyFile {
40     
41     protected final FileChannel JavaDoc channel;
42     
43     public ChannelReadOnlyFile(File JavaDoc file) throws FileNotFoundException JavaDoc {
44         channel = new RandomAccessFile JavaDoc(file, "r").getChannel();
45     }
46
47     public long length() throws IOException JavaDoc {
48         return channel.size();
49     }
50
51     public long getFilePointer() throws IOException JavaDoc {
52         return channel.position();
53     }
54
55     public void seek(final long pos) throws IOException JavaDoc {
56         channel.position(pos);
57     }
58
59     /** For use by {@link #read()} only! */
60     private final ByteBuffer JavaDoc singleByteBuffer = ByteBuffer.allocate(1);
61     
62     public int read() throws IOException JavaDoc {
63         if (channel.read(singleByteBuffer) == 1)
64             return singleByteBuffer.array()[0] & 0xFF;
65         else
66             return -1;
67     }
68
69     public final int read(byte b[]) throws IOException JavaDoc {
70         return read(b, 0, b.length);
71     }
72
73     public int read(final byte[] buf, final int off, final int len)
74     throws IOException JavaDoc {
75         return channel.read(ByteBuffer.wrap(buf, off, len));
76     }
77
78     public void readFully(byte[] b) throws IOException JavaDoc {
79         readFully(b, 0, b.length);
80     }
81
82     public void readFully(byte[] buf, int off, int len) throws IOException JavaDoc {
83         int n = 0;
84     do {
85         int count = read(buf, off + n, len - n);
86         if (count < 0)
87         throw new EOFException JavaDoc();
88         n += count;
89     } while (n < len);
90     }
91
92     public int skipBytes(int n) throws IOException JavaDoc {
93         if (n <= 0)
94             return 0; // for compatibility to RandomAccessFile in case of closed
95

96     final long pos = channel.position();
97     final long len = channel.size();
98     long newPos = pos + n;
99     if (newPos > len)
100         newPos = len;
101     channel.position(newPos);
102
103     return (int) (newPos - pos);
104     }
105     
106     public void close() throws IOException JavaDoc {
107         channel.close();
108     }
109 }
110
Popular Tags