Re-structure LTTng sub-project as per the Linux Tools guidelines
[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 public class BufferedRandomAccessFile extends RandomAccessFile {
22
23 private static final int DEFAULT_BUF_SIZE = 8192;
24 private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8"); //$NON-NLS-1$
25
26 final int BUF_SIZE;
27 byte buffer[];
28 int buf_end = 0;
29 int buf_pos = 0;
30 long real_pos = 0;
31 StringBuilder sb = new StringBuilder();
32
33 public BufferedRandomAccessFile(String name, String mode) throws IOException {
34 this(name, mode, DEFAULT_BUF_SIZE);
35 }
36
37 public BufferedRandomAccessFile(File file, String mode) throws IOException {
38 this(file, mode, DEFAULT_BUF_SIZE);
39 }
40
41 public BufferedRandomAccessFile(String name, String mode, int bufsize) throws IOException {
42 super(name, mode);
43 invalidate();
44 BUF_SIZE = bufsize;
45 buffer = new byte[BUF_SIZE];
46 }
47
48 public BufferedRandomAccessFile(File file, String mode, int bufsize) throws IOException {
49 super(file, mode);
50 invalidate();
51 BUF_SIZE = bufsize;
52 buffer = new byte[BUF_SIZE];
53 }
54
55 @Override
56 public final int read() throws IOException{
57 if (buf_pos >= buf_end) {
58 if (fillBuffer() < 0) {
59 return -1;
60 }
61 }
62 if (buf_end == 0) {
63 return -1;
64 } else {
65 return buffer[buf_pos++];
66 }
67 }
68
69 @Override
70 public int read(byte b[], int off, int len) throws IOException {
71 int leftover = buf_end - buf_pos;
72 if (len <= leftover) {
73 System.arraycopy(buffer, buf_pos, b, off, len);
74 buf_pos += len;
75 return len;
76 }
77 for(int i = 0; i < len; i++) {
78 int c = this.read();
79 if (c != -1) {
80 b[off+i] = (byte) c;
81 } else {
82 return i;
83 }
84 }
85 return len;
86 }
87
88 @Override
89 public long getFilePointer() throws IOException{
90 long l = real_pos;
91 return (l - buf_end + buf_pos);
92 }
93
94 @Override
95 public void seek(long pos) throws IOException {
96 int n = (int) (real_pos - pos);
97 if(n >= 0 && n <= buf_end) {
98 buf_pos = buf_end - n;
99 } else {
100 super.seek(pos);
101 invalidate();
102 }
103 }
104
105 public final String getNextLine() throws IOException {
106 String str = null;
107 if (buf_end - buf_pos <= 0) {
108 if (fillBuffer() < 0) {
109 return null;
110 }
111 }
112 int lineend = -1;
113 for (int i = buf_pos; i < buf_end; i++) {
114 if (buffer[i] == '\n') {
115 lineend = i;
116 break;
117 }
118 }
119 if (lineend < 0) {
120 sb.delete(0, sb.length());
121 int c;
122 while (((c = read()) != -1) && (c != '\n')) {
123 sb.append((char) c);
124 }
125 if ((c == -1) && (sb.length() == 0)) {
126 return null;
127 }
128 if (sb.charAt(sb.length() - 1) == '\r') {
129 sb.deleteCharAt(sb.length() - 1);
130 }
131 return sb.toString();
132 }
133 if (lineend > 0 && buffer[lineend - 1] == '\r' && lineend > buf_pos) {
134 str = new String(buffer, buf_pos, lineend - buf_pos - 1, CHARSET_UTF8);
135 } else {
136 str = new String(buffer, buf_pos, lineend - buf_pos, CHARSET_UTF8);
137 }
138 buf_pos = lineend + 1;
139 return str;
140 }
141
142 private int fillBuffer() throws IOException {
143 int n = super.read(buffer, 0, BUF_SIZE);
144 if (n >= 0) {
145 real_pos += n;
146 buf_end = n;
147 buf_pos = 0;
148 }
149 return n;
150 }
151
152 private void invalidate() throws IOException {
153 buf_end = 0;
154 buf_pos = 0;
155 real_pos = super.getFilePointer();
156 }
157 }
This page took 0.054747 seconds and 6 git commands to generate.