Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6b8b2ef8cb0f12123c302f1119a1ad0409826195 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.io.nio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

import org.eclipse.jetty.io.AbstractBuffer;
import org.eclipse.jetty.io.Buffer;

public class RandomAccessFileBuffer extends AbstractBuffer implements Buffer
{
    final RandomAccessFile _file;
    final FileChannel _channel;
    final int _capacity;

    public RandomAccessFileBuffer(File file) 
        throws FileNotFoundException
    {
        super(READWRITE,true);
        assert file.length()<=Integer.MAX_VALUE;
        _file = new RandomAccessFile(file,"rw");
        _channel=_file.getChannel();
        _capacity=Integer.MAX_VALUE;
        setGetIndex(0);
        setPutIndex((int)file.length());
    }
    
    public RandomAccessFileBuffer(File file,int capacity) 
        throws FileNotFoundException
    {
        super(READWRITE,true);
        assert capacity>=file.length();
        assert file.length()<=Integer.MAX_VALUE;
        _capacity=capacity;
        _file = new RandomAccessFile(file,"rw");
        _channel=_file.getChannel();
        setGetIndex(0);
        setPutIndex((int)file.length());
    }
    
    public RandomAccessFileBuffer(File file,int capacity,int access) 
        throws FileNotFoundException
    {
        super(access,true);
        assert capacity>=file.length();
        assert file.length()<=Integer.MAX_VALUE;
        _capacity=capacity;
        _file = new RandomAccessFile(file,access==READWRITE?"rw":"r");
        _channel=_file.getChannel();
        setGetIndex(0);
        setPutIndex((int)file.length());
    }

    public byte[] array()
    {
        return null;
    }

    public int capacity()
    {
        return _capacity;
    }

    @Override
    public void clear()
    {
        try
        {
            synchronized (_file)
            {
                super.clear();
                _file.setLength(0);
            }
        }
        catch(Exception e)
        {
            throw new RuntimeException(e);
        }
    }


    @Override
    public byte peek()
    {
        synchronized (_file)
        {
            try
            {
                if (_get!=_file.getFilePointer())
                    _file.seek(_get);
                return _file.readByte();
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    public byte peek(int index)
    {
        synchronized (_file)
        {
            try
            {
                _file.seek(index);
                return _file.readByte();
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    public int peek(int index, byte[] b, int offset, int length)
    {
        synchronized (_file)
        {
            try
            {
                _file.seek(index);
                return _file.read(b,offset,length);
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    public void poke(int index, byte b)
    {
        synchronized (_file)
        {
            try
            {
                _file.seek(index);
                _file.writeByte(b);
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public int poke(int index, byte[] b, int offset, int length)
    {
        synchronized (_file)
        {
            try
            {
                _file.seek(index);
                _file.write(b,offset,length);
                return length;
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }
    
    public int writeTo(WritableByteChannel channel,int index, int length)
        throws IOException
    {
        synchronized (_file)
        {
            return (int)_channel.transferTo(index,length,channel);
        }
    }
    
}

Back to the top