Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2010, 2012 Ericsson |
be222f56 PT |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Patrick Tasse - Initial API and implementation, based on article by Nick Zhang | |
11 | * (http://www.javaworld.com/javatips/jw-javatip26.html) | |
12 | ******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.io; | |
15 | ||
16 | import java.io.File; | |
17 | import java.io.IOException; | |
18 | import java.io.RandomAccessFile; | |
19 | import java.nio.charset.Charset; | |
20 | ||
21 | /** | |
22 | * A class to mitigate the Java I/O inefficiency of RandomAccessFile. | |
23 | * | |
24 | * @version 1.0 | |
25 | * @author Patrick Tasse | |
26 | */ | |
27 | public class BufferedRandomAccessFile extends RandomAccessFile { | |
28 | ||
29 | private static final int DEFAULT_BUF_SIZE = 8192; | |
30 | private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8"); //$NON-NLS-1$ | |
31 | ||
32 | final int BUF_SIZE; | |
33 | byte buffer[]; | |
34 | int buf_end = 0; | |
35 | int buf_pos = 0; | |
36 | long real_pos = 0; | |
37 | StringBuilder sb = new StringBuilder(); | |
38 | ||
39 | /** | |
40 | * Constructor using the default buffer size | |
41 | * | |
42 | * @param name | |
43 | * File path. This is passed as-is to the RandomeAccessFile's | |
44 | * constructor. | |
45 | * @param mode | |
46 | * File open mode ("r", "rw", etc.). This is passed as-is to | |
47 | * RandomAccessFile's constructor. | |
48 | * @throws IOException | |
49 | * If the file was not found or couldn't be opened with the | |
50 | * request permissions | |
51 | */ | |
52 | public BufferedRandomAccessFile(String name, String mode) throws IOException { | |
53 | this(name, mode, DEFAULT_BUF_SIZE); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Constructor using the default buffer size | |
58 | * | |
59 | * @param file | |
60 | * File object. This is passed as-is to the RandomeAccessFile's | |
61 | * constructor. | |
62 | * @param mode | |
63 | * File open mode ("r", "rw", etc.). This is passed as-is to | |
64 | * RandomAccessFile's constructor. | |
65 | * @throws IOException | |
66 | * If the file was not found or couldn't be opened with the | |
67 | * request permissions | |
68 | */ | |
69 | public BufferedRandomAccessFile(File file, String mode) throws IOException { | |
70 | this(file, mode, DEFAULT_BUF_SIZE); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Standard constructor. | |
75 | * | |
76 | * @param name | |
77 | * File path. This is passed as-is to the RandomeAccessFile's | |
78 | * constructor. | |
79 | * @param mode | |
80 | * File open mode ("r", "rw", etc.). This is passed as-is to | |
81 | * RandomAccessFile's constructor. | |
82 | * @param bufsize | |
83 | * Buffer size to use, in bytes | |
84 | * @throws IOException | |
85 | * If the file was not found or couldn't be opened with the | |
86 | * request permissions | |
87 | */ | |
88 | public BufferedRandomAccessFile(String name, String mode, int bufsize) throws IOException { | |
89 | super(name, mode); | |
90 | invalidate(); | |
91 | BUF_SIZE = bufsize; | |
92 | buffer = new byte[BUF_SIZE]; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Standard constructor. | |
97 | * | |
98 | * @param file | |
99 | * File object. This is passed as-is to the RandomeAccessFile's | |
100 | * constructor. | |
101 | * @param mode | |
102 | * File open mode ("r", "rw", etc.). This is passed as-is to | |
103 | * RandomAccessFile's constructor. | |
104 | * @param bufsize | |
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 | |
109 | */ | |
110 | public BufferedRandomAccessFile(File file, String mode, int bufsize) throws IOException { | |
111 | super(file, mode); | |
112 | invalidate(); | |
113 | BUF_SIZE = bufsize; | |
114 | buffer = new byte[BUF_SIZE]; | |
115 | } | |
116 | ||
117 | @Override | |
118 | public final int read() throws IOException{ | |
119 | if (buf_pos >= buf_end) { | |
120 | if (fillBuffer() < 0) { | |
121 | return -1; | |
122 | } | |
123 | } | |
124 | if (buf_end == 0) { | |
125 | return -1; | |
126 | } | |
f3105f09 | 127 | return (buffer[buf_pos++] & 0xff); |
be222f56 PT |
128 | } |
129 | ||
130 | @Override | |
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); | |
135 | buf_pos += len; | |
136 | return len; | |
137 | } | |
138 | for(int i = 0; i < len; i++) { | |
139 | int c = this.read(); | |
140 | if (c != -1) { | |
141 | b[off+i] = (byte) c; | |
142 | } else { | |
143 | return i; | |
144 | } | |
145 | } | |
146 | return len; | |
147 | } | |
148 | ||
149 | @Override | |
150 | public long getFilePointer() throws IOException{ | |
151 | long l = real_pos; | |
152 | return (l - buf_end + buf_pos); | |
153 | } | |
154 | ||
155 | @Override | |
156 | public void seek(long pos) throws IOException { | |
157 | int n = (int) (real_pos - pos); | |
158 | if(n >= 0 && n <= buf_end) { | |
159 | buf_pos = buf_end - n; | |
160 | } else { | |
161 | super.seek(pos); | |
162 | invalidate(); | |
163 | } | |
164 | } | |
165 | ||
166 | /** | |
167 | * Read the next line from the buffer (ie, until the next '\n'). The bytes | |
168 | * are interpreted as UTF-8 characters. | |
169 | * | |
170 | * @return The String that was read | |
171 | * @throws IOException | |
172 | * If we failed reading the file | |
173 | */ | |
174 | public final String getNextLine() throws IOException { | |
175 | String str = null; | |
176 | if (buf_end - buf_pos <= 0) { | |
177 | if (fillBuffer() < 0) { | |
178 | return null; | |
179 | } | |
180 | } | |
181 | int lineend = -1; | |
182 | for (int i = buf_pos; i < buf_end; i++) { | |
183 | if (buffer[i] == '\n') { | |
184 | lineend = i; | |
185 | break; | |
186 | } | |
187 | } | |
188 | if (lineend < 0) { | |
189 | sb.delete(0, sb.length()); | |
190 | int c; | |
191 | while (((c = read()) != -1) && (c != '\n')) { | |
192 | sb.append((char) c); | |
193 | } | |
194 | if ((c == -1) && (sb.length() == 0)) { | |
195 | return null; | |
196 | } | |
197 | if (sb.charAt(sb.length() - 1) == '\r') { | |
198 | sb.deleteCharAt(sb.length() - 1); | |
199 | } | |
200 | return sb.toString(); | |
201 | } | |
202 | if (lineend > 0 && buffer[lineend - 1] == '\r' && lineend > buf_pos) { | |
203 | str = new String(buffer, buf_pos, lineend - buf_pos - 1, CHARSET_UTF8); | |
204 | } else { | |
205 | str = new String(buffer, buf_pos, lineend - buf_pos, CHARSET_UTF8); | |
206 | } | |
207 | buf_pos = lineend + 1; | |
208 | return str; | |
209 | } | |
210 | ||
211 | private int fillBuffer() throws IOException { | |
212 | int n = super.read(buffer, 0, BUF_SIZE); | |
213 | if (n >= 0) { | |
214 | real_pos += n; | |
215 | buf_end = n; | |
216 | buf_pos = 0; | |
217 | } | |
218 | return n; | |
219 | } | |
220 | ||
221 | private void invalidate() throws IOException { | |
222 | buf_end = 0; | |
223 | buf_pos = 0; | |
224 | real_pos = super.getFilePointer(); | |
225 | } | |
226 | } |