1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation, based on article by Nick Zhang
11 * (http://www.javaworld.com/javatips/jw-javatip26.html)
12 ******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.io
;
17 import java
.io
.IOException
;
18 import java
.io
.RandomAccessFile
;
19 import java
.nio
.charset
.Charset
;
22 * A class to mitigate the Java I/O inefficiency of RandomAccessFile.
25 * @author Patrick Tasse
27 public class BufferedRandomAccessFile
extends RandomAccessFile
{
29 private static final int DEFAULT_BUF_SIZE
= 8192;
30 private static final Charset CHARSET_UTF8
= Charset
.forName("UTF-8"); //$NON-NLS-1$
32 private final int BUF_SIZE
;
33 private final byte buffer
[];
34 private int buf_end
= 0;
35 private int buf_pos
= 0;
36 private long real_pos
= 0;
37 private final StringBuilder sb
= new StringBuilder();
40 * Constructor using the default buffer size
43 * File path. This is passed as-is to the RandomAccessFile's
46 * File open mode ("r", "rw", etc.). This is passed as-is to
47 * RandomAccessFile's constructor.
49 * If the file was not found or couldn't be opened with the
52 public BufferedRandomAccessFile(String name
, String mode
) throws IOException
{
53 this(name
, mode
, DEFAULT_BUF_SIZE
);
57 * Constructor using the default buffer size
60 * File object. This is passed as-is to the RandomAccessFile's
63 * File open mode ("r", "rw", etc.). This is passed as-is to
64 * RandomAccessFile's constructor.
66 * If the file was not found or couldn't be opened with the
69 public BufferedRandomAccessFile(File file
, String mode
) throws IOException
{
70 this(file
, mode
, DEFAULT_BUF_SIZE
);
74 * Standard constructor.
77 * File path. This is passed as-is to the RandomAccessFile's
80 * File open mode ("r", "rw", etc.). This is passed as-is to
81 * RandomAccessFile's constructor.
83 * Buffer size to use, in bytes
85 * If the file was not found or couldn't be opened with the
88 public BufferedRandomAccessFile(String name
, String mode
, int bufsize
) throws IOException
{
92 buffer
= new byte[BUF_SIZE
];
96 * Standard constructor.
99 * File object. This is passed as-is to the RandomAccessFile's
102 * File open mode ("r", "rw", etc.). This is passed as-is to
103 * RandomAccessFile's constructor.
105 * Buffer size to use, in bytes
106 * @throws IOException
107 * If the file was not found or couldn't be opened with the
108 * request permissions
110 public BufferedRandomAccessFile(File file
, String mode
, int bufsize
) throws IOException
{
114 buffer
= new byte[BUF_SIZE
];
118 public final int read() throws IOException
{
119 if (buf_pos
>= buf_end
) {
120 if (fillBuffer() < 0) {
127 return (buffer
[buf_pos
++] & 0xff);
131 public int read(byte b
[], int off
, int len
) throws IOException
{
132 int leftover
= buf_end
- buf_pos
;
133 if (len
<= leftover
) {
134 System
.arraycopy(buffer
, buf_pos
, b
, off
, len
);
138 for (int i
= 0; i
< len
; i
++) {
141 b
[off
+ i
] = (byte) c
;
153 public long getFilePointer() throws IOException
{
155 return (l
- buf_end
+ buf_pos
);
159 public void seek(long pos
) throws IOException
{
160 int n
= (int) (real_pos
- pos
);
161 if (n
>= 0 && n
<= buf_end
) {
162 buf_pos
= buf_end
- n
;
170 * Read the next line from the buffer (ie, until the next '\n'). The bytes
171 * are interpreted as UTF-8 characters.
173 * @return The String that was read
174 * @throws IOException
175 * If we failed reading the file
177 public final String
getNextLine() throws IOException
{
179 if (buf_end
- buf_pos
<= 0) {
180 if (fillBuffer() < 0) {
185 for (int i
= buf_pos
; i
< buf_end
; i
++) {
186 if (buffer
[i
] == '\n') {
192 sb
.delete(0, sb
.length());
194 while (((c
= read()) != -1) && (c
!= '\n')) {
197 if ((c
== -1) && (sb
.length() == 0)) {
200 if (sb
.charAt(sb
.length() - 1) == '\r') {
201 sb
.deleteCharAt(sb
.length() - 1);
203 return sb
.toString();
205 if (lineend
> 0 && buffer
[lineend
- 1] == '\r' && lineend
> buf_pos
) {
206 str
= new String(buffer
, buf_pos
, lineend
- buf_pos
- 1, CHARSET_UTF8
);
208 str
= new String(buffer
, buf_pos
, lineend
- buf_pos
, CHARSET_UTF8
);
210 buf_pos
= lineend
+ 1;
214 private int fillBuffer() throws IOException
{
215 int n
= super.read(buffer
, 0, BUF_SIZE
);
224 private void invalidate() throws IOException
{
227 real_pos
= super.getFilePointer();
This page took 0.043173 seconds and 5 git commands to generate.