/[xulu]/trunk/src/appl/parallel/data/xulugridfile/BufferedRandomAccessFile.java
ViewVC logotype

Contents of /trunk/src/appl/parallel/data/xulugridfile/BufferedRandomAccessFile.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Wed Feb 25 11:54:01 2009 UTC (15 years, 9 months ago) by mojays
File size: 2765 byte(s)
First Commit, corresponds to Revision 1008 of Wikisquare-SVN 
1 package appl.parallel.data.xulugridfile;
2
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
5
6 /**
7 * Copied from java world.
8 *
9 * See http://www.javaworld.com/javaworld/javatips/jw-javatip26.html
10 *
11 * @author Nick Zhang
12 *
13 */
14 public class BufferedRandomAccessFile extends RandomAccessFile {
15
16 byte buffer[];
17
18 int buf_end = 0;
19
20 int buf_pos = 0;
21
22 long real_pos = 0;
23
24 private int BUF_SIZE;
25
26 public BufferedRandomAccessFile(String filename, String mode, int bufsize)
27 throws IOException {
28 super(filename, mode);
29 invalidate();
30 BUF_SIZE = bufsize;
31 buffer = new byte[BUF_SIZE];
32 }
33
34 public final int read() throws IOException {
35 if (buf_pos >= buf_end) {
36 if (fillBuffer() < 0)
37 return -1;
38 }
39 if (buf_end == 0) {
40 return -1;
41 } else {
42 return buffer[buf_pos++];
43 }
44 }
45
46 private int fillBuffer() throws IOException {
47 int n = super.read(buffer, 0, BUF_SIZE);
48 if (n >= 0) {
49 real_pos += n;
50 buf_end = n;
51 buf_pos = 0;
52 }
53 return n;
54 }
55
56 private void invalidate() throws IOException {
57 buf_end = 0;
58 buf_pos = 0;
59 real_pos = super.getFilePointer();
60 }
61
62 public int read(byte b[], int off, int len) throws IOException {
63 int leftover = buf_end - buf_pos;
64 if (len <= leftover) {
65 System.arraycopy(buffer, buf_pos, b, off, len);
66 buf_pos += len;
67 return len;
68 }
69 for (int i = 0; i < len; i++) {
70 int c = this.read();
71 if (c != -1)
72 b[off + i] = (byte) c;
73 else {
74 return i;
75 }
76 }
77 return len;
78 }
79
80 public long getFilePointer() throws IOException {
81 long l = real_pos;
82 return (l - buf_end + buf_pos);
83 }
84
85 public void seek(long pos) throws IOException {
86 int n = (int) (real_pos - pos);
87 if (n >= 0 && n <= buf_end) {
88 buf_pos = buf_end - n;
89 } else {
90 super.seek(pos);
91 invalidate();
92 }
93 }
94
95 /**
96 * return a next line in String
97 */
98 public final String getNextLine() throws IOException {
99 String str = null;
100 if (buf_end - buf_pos <= 0) {
101 if (fillBuffer() < 0) {
102 throw new IOException("error in filling buffer!");
103 }
104 }
105 int lineend = -1;
106 for (int i = buf_pos; i < buf_end; i++) {
107 if (buffer[i] == '\n') {
108 lineend = i;
109 break;
110 }
111 }
112 if (lineend < 0) {
113 StringBuffer input = new StringBuffer(256);
114 int c;
115 while (((c = read()) != -1) && (c != '\n')) {
116 input.append((char) c);
117 }
118 if ((c == -1) && (input.length() == 0)) {
119 return null;
120 }
121 return input.toString();
122 }
123 if (lineend > 0 && buffer[lineend - 1] == '\r')
124 str = new String(buffer, 0, buf_pos, lineend - buf_pos - 1);
125 else
126 str = new String(buffer, 0, buf_pos, lineend - buf_pos);
127 buf_pos = lineend + 1;
128 return str;
129 }
130 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26