• R/O
  • SSH
  • HTTPS

copper: Commit


Commit MetaInfo

Revision1583 (tree)
Time2018-12-25 14:44:43
Authormiyabe

Log Message

(empty log message)

Change Summary

Incremental Difference

--- copper/trunk/sakae/zs-font/src/main/java/net/zamasoft/font/util/BufferedRandomAccessFile.java (revision 1582)
+++ copper/trunk/sakae/zs-font/src/main/java/net/zamasoft/font/util/BufferedRandomAccessFile.java (revision 1583)
@@ -22,183 +22,176 @@
2222
2323 /**
2424 * This class is a version of the one published at
25- * https://code.google.com/p/jmzreader/wiki/BufferedRandomAccessFile augmented to handle unsigned
26- * bytes. The original class is published under Apache 2.0 license. Fix is marked below
25+ * https://code.google.com/p/jmzreader/wiki/BufferedRandomAccessFile augmented
26+ * to handle unsigned bytes. The original class is published under Apache 2.0
27+ * license. Fix is marked below
2728 *
28- * This is an optimized version of the RandomAccessFile class as described by Nick Zhang on
29- * JavaWorld.com. The article can be found at
29+ * This is an optimized version of the RandomAccessFile class as described by
30+ * Nick Zhang on JavaWorld.com. The article can be found at
3031 * http://www.javaworld.com/javaworld/javatips/jw-javatip26.html
3132 *
3233 * @author jg
3334 */
34-public class BufferedRandomAccessFile extends RandomAccessFile
35-{
36- /**
37- * Uses a byte instead of a char buffer for efficiency reasons.
38- */
39- private final byte buffer[];
40- private int bufend = 0;
41- private int bufpos = 0;
42-
43- /**
44- * The position inside the actual file.
45- */
46- private long realpos = 0;
47-
48- /**
49- * Buffer size.
50- */
51- private final int BUFSIZE;
35+public class BufferedRandomAccessFile extends RandomAccessFile {
36+ /**
37+ * Uses a byte instead of a char buffer for efficiency reasons.
38+ */
39+ private final byte buffer[];
40+ private int bufend = 0;
41+ private int bufpos = 0;
5242
53- /**
54- * Creates a new instance of the BufferedRandomAccessFile.
55- *
56- * @param filename The path of the file to open.
57- * @param mode Specifies the mode to use ("r", "rw", etc.) See the BufferedLineReader
58- * documentation for more information.
59- * @param bufsize The buffer size (in bytes) to use.
60- * @throws FileNotFoundException If the mode is "r" but the given string does not denote an
61- * existing regular file, or if the mode begins with "rw" but the given string does not denote
62- * an existing, writable regular file and a new regular file of that name cannot be created, or
63- * if some other error occurs while opening or creating the file.
64- */
65- public BufferedRandomAccessFile(String filename, String mode, int bufsize)
66- throws FileNotFoundException
67- {
68- super(filename, mode);
69- BUFSIZE = bufsize;
70- buffer = new byte[BUFSIZE];
71- }
43+ /**
44+ * The position inside the actual file.
45+ */
46+ private long realpos = 0;
7247
73- /**
74- * Creates a new instance of the BufferedRandomAccessFile.
75- *
76- * @param file The file to open.
77- * @param mode Specifies the mode to use ("r", "rw", etc.) See the BufferedLineReader
78- * documentation for more information.
79- * @throws FileNotFoundException If the mode is "r" but the given file path does not denote an
80- * existing regular file, or if the mode begins with "rw" but the given file path does not denote
81- * an existing, writable regular file and a new regular file of that name cannot be created, or
82- * if some other error occurs while opening or creating the file.
83- */
84- public BufferedRandomAccessFile(File file, String mode)
85- throws FileNotFoundException
86- {
87- super(file, mode);
88- BUFSIZE = 8192;
89- buffer = new byte[BUFSIZE];
90- }
48+ /**
49+ * Buffer size.
50+ */
51+ private final int BUFSIZE;
9152
92- /**
93- * {@inheritDoc}
94- */
95- @Override
96- public final int read() throws IOException
97- {
98- if (bufpos >= bufend && fillBuffer() < 0)
99- {
100- return -1;
101- }
102- if (bufend == 0)
103- {
104- return -1;
105- }
106- // FIX to handle unsigned bytes
107- return (buffer[bufpos++] + 256) & 0xFF;
108- // End of fix
109- }
53+ /**
54+ * Creates a new instance of the BufferedRandomAccessFile.
55+ *
56+ * @param filename
57+ * The path of the file to open.
58+ * @param mode
59+ * Specifies the mode to use ("r", "rw", etc.) See the
60+ * BufferedLineReader documentation for more information.
61+ * @param bufsize
62+ * The buffer size (in bytes) to use.
63+ * @throws FileNotFoundException
64+ * If the mode is "r" but the given string does not denote an
65+ * existing regular file, or if the mode begins with "rw" but the
66+ * given string does not denote an existing, writable regular file
67+ * and a new regular file of that name cannot be created, or if some
68+ * other error occurs while opening or creating the file.
69+ */
70+ public BufferedRandomAccessFile(String filename, String mode, int bufsize) throws FileNotFoundException {
71+ super(filename, mode);
72+ BUFSIZE = bufsize;
73+ buffer = new byte[BUFSIZE];
74+ }
11075
111- /**
112- * Reads the next BUFSIZE bytes into the internal buffer.
113- *
114- * @return The total number of bytes read into the buffer, or -1 if there is no more data
115- * because the end of the file has been reached.
116- * @throws IOException If the first byte cannot be read for any reason other than end of file,
117- * or if the random access file has been closed, or if some other I/O error occurs.
118- */
119- private int fillBuffer() throws IOException
120- {
121- int n = super.read(buffer, 0, BUFSIZE);
76+ /**
77+ * Creates a new instance of the BufferedRandomAccessFile.
78+ *
79+ * @param file
80+ * The file to open.
81+ * @param mode
82+ * Specifies the mode to use ("r", "rw", etc.) See the
83+ * BufferedLineReader documentation for more information.
84+ * @throws FileNotFoundException
85+ * If the mode is "r" but the given file path does not denote an
86+ * existing regular file, or if the mode begins with "rw" but the
87+ * given file path does not denote an existing, writable regular
88+ * file and a new regular file of that name cannot be created, or if
89+ * some other error occurs while opening or creating the file.
90+ */
91+ public BufferedRandomAccessFile(File file, String mode) throws FileNotFoundException {
92+ super(file, mode);
93+ BUFSIZE = 512;
94+ buffer = new byte[BUFSIZE];
95+ }
12296
123- if (n >= 0)
124- {
125- realpos += n;
126- bufend = n;
127- bufpos = 0;
128- }
129- return n;
130- }
97+ /**
98+ * {@inheritDoc}
99+ */
100+ @Override
101+ public final int read() throws IOException {
102+ if (bufpos >= bufend && fillBuffer() < 0) {
103+ return -1;
104+ }
105+ if (bufend == 0) {
106+ return -1;
107+ }
108+ // FIX to handle unsigned bytes
109+ return (buffer[bufpos++] + 256) & 0xFF;
110+ // End of fix
111+ }
131112
132- /**
133- * Clears the local buffer.
134- *
135- * @throws IOException If an I/O error occurs.
136- */
137- private void invalidate() throws IOException
138- {
139- bufend = 0;
140- bufpos = 0;
141- realpos = super.getFilePointer();
142- }
113+ /**
114+ * Reads the next BUFSIZE bytes into the internal buffer.
115+ *
116+ * @return The total number of bytes read into the buffer, or -1 if there is no
117+ * more data because the end of the file has been reached.
118+ * @throws IOException
119+ * If the first byte cannot be read for any reason other than end of
120+ * file, or if the random access file has been closed, or if some
121+ * other I/O error occurs.
122+ */
123+ private int fillBuffer() throws IOException {
124+ int n = super.read(buffer, 0, BUFSIZE);
143125
144- /**
145- * {@inheritDoc}
146- */
147- @Override
148- public int read(byte b[]) throws IOException {
149- return this.read(b, 0, b.length);
150- }
151-
152- /**
153- * {@inheritDoc}
154- */
155- @Override
156- public int read(byte b[], int off, int len) throws IOException
157- {
158- int leftover = bufend - bufpos;
159- if (len <= leftover)
160- {
161- System.arraycopy(buffer, bufpos, b, off, len);
162- bufpos += len;
163- return len;
164- }
165- System.arraycopy(buffer, bufpos, b, off, leftover);
166- bufpos += leftover;
167- if (fillBuffer() > 0)
168- {
169- int bytesRead = read(b, off + leftover, len - leftover);
170- if (bytesRead > 0)
171- {
172- leftover += bytesRead;
173- }
174- }
175- return leftover > 0 ? leftover : -1;
176- }
126+ if (n >= 0) {
127+ realpos += n;
128+ bufend = n;
129+ bufpos = 0;
130+ }
131+ return n;
132+ }
177133
178- /**
179- * {@inheritDoc}
180- */
181- @Override
182- public long getFilePointer() throws IOException
183- {
184- return realpos - bufend + bufpos;
185- }
134+ /**
135+ * Clears the local buffer.
136+ *
137+ * @throws IOException
138+ * If an I/O error occurs.
139+ */
140+ private void invalidate() throws IOException {
141+ bufend = 0;
142+ bufpos = 0;
143+ realpos = super.getFilePointer();
144+ }
186145
187- /**
188- * {@inheritDoc}
189- */
190- @Override
191- public void seek(long pos) throws IOException
192- {
193- int n = (int) (realpos - pos);
194- if (n >= 0 && n <= bufend)
195- {
196- bufpos = bufend - n;
197- }
198- else
199- {
200- super.seek(pos);
201- invalidate();
202- }
203- }
146+ /**
147+ * {@inheritDoc}
148+ */
149+ @Override
150+ public int read(byte b[]) throws IOException {
151+ return this.read(b, 0, b.length);
152+ }
153+
154+ /**
155+ * {@inheritDoc}
156+ */
157+ @Override
158+ public int read(byte b[], int off, int len) throws IOException {
159+ int leftover = bufend - bufpos;
160+ if (len <= leftover) {
161+ System.arraycopy(buffer, bufpos, b, off, len);
162+ bufpos += len;
163+ return len;
164+ }
165+ System.arraycopy(buffer, bufpos, b, off, leftover);
166+ bufpos += leftover;
167+ if (fillBuffer() > 0) {
168+ int bytesRead = read(b, off + leftover, len - leftover);
169+ if (bytesRead > 0) {
170+ leftover += bytesRead;
171+ }
172+ }
173+ return leftover > 0 ? leftover : -1;
174+ }
175+
176+ /**
177+ * {@inheritDoc}
178+ */
179+ @Override
180+ public long getFilePointer() throws IOException {
181+ return realpos - bufend + bufpos;
182+ }
183+
184+ /**
185+ * {@inheritDoc}
186+ */
187+ @Override
188+ public void seek(long pos) throws IOException {
189+ int n = (int) (realpos - pos);
190+ if (n >= 0 && n <= bufend) {
191+ bufpos = bufend - n;
192+ } else {
193+ super.seek(pos);
194+ invalidate();
195+ }
196+ }
204197 }
\ No newline at end of file
Show on old repository browser