tmf: Simple warning fixes in tmf.core and tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / io / BufferedRandomAccessFile.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
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 public BufferedRandomAccessFile(String name, String mode) throws IOException {
40 this(name, mode, DEFAULT_BUF_SIZE);
41 }
42
43 public BufferedRandomAccessFile(File file, String mode) throws IOException {
44 this(file, mode, DEFAULT_BUF_SIZE);
45 }
46
47 public BufferedRandomAccessFile(String name, String mode, int bufsize) throws IOException {
48 super(name, mode);
49 invalidate();
50 BUF_SIZE = bufsize;
51 buffer = new byte[BUF_SIZE];
52 }
53
54 public BufferedRandomAccessFile(File file, String mode, int bufsize) throws IOException {
55 super(file, mode);
56 invalidate();
57 BUF_SIZE = bufsize;
58 buffer = new byte[BUF_SIZE];
59 }
60
61 @Override
62 public final int read() throws IOException{
63 if (buf_pos >= buf_end) {
64 if (fillBuffer() < 0) {
65 return -1;
66 }
67 }
68 if (buf_end == 0) {
69 return -1;
70 }
71 return buffer[buf_pos++];
72 }
73
74 @Override
75 public int read(byte b[], int off, int len) throws IOException {
76 int leftover = buf_end - buf_pos;
77 if (len <= leftover) {
78 System.arraycopy(buffer, buf_pos, b, off, len);
79 buf_pos += len;
80 return len;
81 }
82 for(int i = 0; i < len; i++) {
83 int c = this.read();
84 if (c != -1) {
85 b[off+i] = (byte) c;
86 } else {
87 return i;
88 }
89 }
90 return len;
91 }
92
93 @Override
94 public long getFilePointer() throws IOException{
95 long l = real_pos;
96 return (l - buf_end + buf_pos);
97 }
98
99 @Override
100 public void seek(long pos) throws IOException {
101 int n = (int) (real_pos - pos);
102 if(n >= 0 && n <= buf_end) {
103 buf_pos = buf_end - n;
104 } else {
105 super.seek(pos);
106 invalidate();
107 }
108 }
109
110 public final String getNextLine() throws IOException {
111 String str = null;
112 if (buf_end - buf_pos <= 0) {
113 if (fillBuffer() < 0) {
114 return null;
115 }
116 }
117 int lineend = -1;
118 for (int i = buf_pos; i < buf_end; i++) {
119 if (buffer[i] == '\n') {
120 lineend = i;
121 break;
122 }
123 }
124 if (lineend < 0) {
125 sb.delete(0, sb.length());
126 int c;
127 while (((c = read()) != -1) && (c != '\n')) {
128 sb.append((char) c);
129 }
130 if ((c == -1) && (sb.length() == 0)) {
131 return null;
132 }
133 if (sb.charAt(sb.length() - 1) == '\r') {
134 sb.deleteCharAt(sb.length() - 1);
135 }
136 return sb.toString();
137 }
138 if (lineend > 0 && buffer[lineend - 1] == '\r' && lineend > buf_pos) {
139 str = new String(buffer, buf_pos, lineend - buf_pos - 1, CHARSET_UTF8);
140 } else {
141 str = new String(buffer, buf_pos, lineend - buf_pos, CHARSET_UTF8);
142 }
143 buf_pos = lineend + 1;
144 return str;
145 }
146
147 private int fillBuffer() throws IOException {
148 int n = super.read(buffer, 0, BUF_SIZE);
149 if (n >= 0) {
150 real_pos += n;
151 buf_end = n;
152 buf_pos = 0;
153 }
154 return n;
155 }
156
157 private void invalidate() throws IOException {
158 buf_end = 0;
159 buf_pos = 0;
160 real_pos = super.getFilePointer();
161 }
162 }
This page took 0.036621 seconds and 6 git commands to generate.