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 @Override
80 public void clear()
81 {
82 try
83 {
84 synchronized (_file)
85 {
86 super.clear();
87 _file.setLength(0);
88 }
89 }
90 catch(Exception e)
91 {
92 throw new RuntimeException(e);
93 }
94 }
95
96
97 @Override
98 public byte peek()
99 {
100 synchronized (_file)
101 {
102 try
103 {
104 if (_get!=_file.getFilePointer())
105 _file.seek(_get);
106 return _file.readByte();
107 }
108 catch(Exception e)
109 {
110 throw new RuntimeException(e);
111 }
112 }
113 }
114
115 public byte peek(int index)
116 {
117 synchronized (_file)
118 {
119 try
120 {
121 _file.seek(index);
122 return _file.readByte();
123 }
124 catch(Exception e)
125 {
126 throw new RuntimeException(e);
127 }
128 }
129 }
130
131 public int peek(int index, byte[] b, int offset, int length)
132 {
133 synchronized (_file)
134 {
135 try
136 {
137 _file.seek(index);
138 return _file.read(b,offset,length);
139 }
140 catch(Exception e)
141 {
142 throw new RuntimeException(e);
143 }
144 }
145 }
146
147 public void poke(int index, byte b)
148 {
149 synchronized (_file)
150 {
151 try
152 {
153 _file.seek(index);
154 _file.writeByte(b);
155 }
156 catch(Exception e)
157 {
158 throw new RuntimeException(e);
159 }
160 }
161 }
162
163 @Override
164 public int poke(int index, byte[] b, int offset, int length)
165 {
166 synchronized (_file)
167 {
168 try
169 {
170 _file.seek(index);
171 _file.write(b,offset,length);
172 return length;
173 }
174 catch(Exception e)
175 {
176 throw new RuntimeException(e);
177 }
178 }
179 }
180
181 public int writeTo(WritableByteChannel channel,int index, int length)
182 throws IOException
183 {
184 synchronized (_file)
185 {
186 return (int)_channel.transferTo(index,length,channel);
187 }
188 }
189
190 }