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