1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.io.nio;
14
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.RandomAccessFile;
19 import java.nio.channels.FileChannel;
20 import java.nio.channels.WritableByteChannel;
21
22 import org.eclipse.jetty.io.AbstractBuffer;
23 import org.eclipse.jetty.io.Buffer;
24
25 public class RandomAccessFileBuffer extends AbstractBuffer implements Buffer
26 {
27 final RandomAccessFile _file;
28 final FileChannel _channel;
29 final int _capacity;
30
31 public RandomAccessFileBuffer(File file)
32 throws FileNotFoundException
33 {
34 super(READWRITE,true);
35 assert file.length()<=Integer.MAX_VALUE;
36 _file = new RandomAccessFile(file,"rw");
37 _channel=_file.getChannel();
38 _capacity=Integer.MAX_VALUE;
39 setGetIndex(0);
40 setPutIndex((int)file.length());
41 }
42
43 public RandomAccessFileBuffer(File file,int capacity)
44 throws FileNotFoundException
45 {
46 super(READWRITE,true);
47 assert capacity>=file.length();
48 assert file.length()<=Integer.MAX_VALUE;
49 _capacity=capacity;
50 _file = new RandomAccessFile(file,"rw");
51 _channel=_file.getChannel();
52 setGetIndex(0);
53 setPutIndex((int)file.length());
54 }
55
56 public RandomAccessFileBuffer(File file,int capacity,int access)
57 throws FileNotFoundException
58 {
59 super(access,true);
60 assert capacity>=file.length();
61 assert file.length()<=Integer.MAX_VALUE;
62 _capacity=capacity;
63 _file = new RandomAccessFile(file,access==READWRITE?"rw":"r");
64 _channel=_file.getChannel();
65 setGetIndex(0);
66 setPutIndex((int)file.length());
67 }
68
69 public byte[] array()
70 {
71 return null;
72 }
73
74 public int capacity()
75 {
76 return _capacity;
77 }
78
79 public void clear()
80 {
81 try
82 {
83 synchronized (_file)
84 {
85 super.clear();
86 _file.setLength(0);
87 }
88 }
89 catch(Exception e)
90 {
91 throw new RuntimeException(e);
92 }
93 }
94
95
96 public byte peek()
97 {
98 synchronized (_file)
99 {
100 try
101 {
102 if (_get!=_file.getFilePointer())
103 _file.seek(_get);
104 return _file.readByte();
105 }
106 catch(Exception e)
107 {
108 throw new RuntimeException(e);
109 }
110 }
111 }
112
113 public byte peek(int index)
114 {
115 synchronized (_file)
116 {
117 try
118 {
119 _file.seek(index);
120 return _file.readByte();
121 }
122 catch(Exception e)
123 {
124 throw new RuntimeException(e);
125 }
126 }
127 }
128
129 public int peek(int index, byte[] b, int offset, int length)
130 {
131 synchronized (_file)
132 {
133 try
134 {
135 _file.seek(index);
136 return _file.read(b,offset,length);
137 }
138 catch(Exception e)
139 {
140 throw new RuntimeException(e);
141 }
142 }
143 }
144
145 public void poke(int index, byte b)
146 {
147 synchronized (_file)
148 {
149 try
150 {
151 _file.seek(index);
152 _file.writeByte(b);
153 }
154 catch(Exception e)
155 {
156 throw new RuntimeException(e);
157 }
158 }
159 }
160
161 public int poke(int index, byte[] b, int offset, int length)
162 {
163 synchronized (_file)
164 {
165 try
166 {
167 _file.seek(index);
168 _file.write(b,offset,length);
169 return length;
170 }
171 catch(Exception e)
172 {
173 throw new RuntimeException(e);
174 }
175 }
176 }
177
178 public int writeTo(WritableByteChannel channel,int index, int length)
179 throws IOException
180 {
181 synchronized (_file)
182 {
183 return (int)_channel.transferTo(index,length,channel);
184 }
185 }
186
187 }