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