Fix for Bug337900
authorFrancois Chouinard <fchouinard@gmail.com>
Fri, 11 Mar 2011 18:57:57 +0000 (13:57 -0500)
committerFrancois Chouinard <fchouinard@gmail.com>
Fri, 11 Mar 2011 18:57:57 +0000 (13:57 -0500)
org.eclipse.linuxtools.tmf/META-INF/MANIFEST.MF
org.eclipse.linuxtools.tmf/src/org/eclipse/linuxtools/tmf/io/BufferedRandomAccessFile.java [new file with mode: 0644]

index 3626e69efca00fc49510c5c1333f521f9153e6af..f7224263e3011b737abf80e0e4490b6fd6d5f19f 100644 (file)
@@ -13,6 +13,7 @@ Export-Package: org.eclipse.linuxtools.tmf,
  org.eclipse.linuxtools.tmf.component,
  org.eclipse.linuxtools.tmf.event,
  org.eclipse.linuxtools.tmf.experiment,
+ org.eclipse.linuxtools.tmf.io,
  org.eclipse.linuxtools.tmf.parser,
  org.eclipse.linuxtools.tmf.request,
  org.eclipse.linuxtools.tmf.signal,
diff --git a/org.eclipse.linuxtools.tmf/src/org/eclipse/linuxtools/tmf/io/BufferedRandomAccessFile.java b/org.eclipse.linuxtools.tmf/src/org/eclipse/linuxtools/tmf/io/BufferedRandomAccessFile.java
new file mode 100644 (file)
index 0000000..435952f
--- /dev/null
@@ -0,0 +1,157 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Ericsson\r
+ * \r
+ * All rights reserved. This program and the accompanying materials are\r
+ * made available under the terms of the Eclipse Public License v1.0 which\r
+ * accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ * \r
+ * Contributors:\r
+ *   Patrick Tasse - Initial API and implementation, based on article by Nick Zhang\r
+ *                   (http://www.javaworld.com/javatips/jw-javatip26.html)\r
+ ******************************************************************************/\r
+\r
+package org.eclipse.linuxtools.tmf.io;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.RandomAccessFile;\r
+import java.nio.charset.Charset;\r
+\r
+public class BufferedRandomAccessFile extends RandomAccessFile {\r
+\r
+       private static final int DEFAULT_BUF_SIZE = 8192;\r
+       private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8"); //$NON-NLS-1$\r
+       \r
+       final int BUF_SIZE;\r
+       byte buffer[];\r
+       int buf_end = 0;\r
+       int buf_pos = 0;\r
+       long real_pos = 0;\r
+       StringBuilder sb = new StringBuilder();\r
+       \r
+       public BufferedRandomAccessFile(String name, String mode) throws IOException {\r
+               this(name, mode, DEFAULT_BUF_SIZE);\r
+       }\r
+       \r
+       public BufferedRandomAccessFile(File file, String mode) throws IOException {\r
+               this(file, mode, DEFAULT_BUF_SIZE);\r
+       }\r
+       \r
+       public BufferedRandomAccessFile(String name, String mode, int bufsize) throws IOException {\r
+           super(name, mode);\r
+           invalidate();\r
+           BUF_SIZE = bufsize;\r
+           buffer = new byte[BUF_SIZE];\r
+    }\r
+\r
+       public BufferedRandomAccessFile(File file, String mode, int bufsize) throws IOException {\r
+           super(file, mode);\r
+           invalidate();\r
+           BUF_SIZE = bufsize;\r
+           buffer = new byte[BUF_SIZE];\r
+    }\r
+\r
+       @Override\r
+    public final int read() throws IOException{\r
+               if (buf_pos >= buf_end) {\r
+                       if (fillBuffer() < 0) {\r
+                               return -1;\r
+                       }\r
+               }\r
+               if (buf_end == 0) {\r
+                       return -1;\r
+               } else {\r
+                       return buffer[buf_pos++];\r
+               }\r
+       }\r
+       \r
+       @Override\r
+    public int read(byte b[], int off, int len) throws IOException {\r
+               int leftover = buf_end - buf_pos;\r
+               if (len <= leftover) {\r
+                       System.arraycopy(buffer, buf_pos, b, off, len);\r
+                       buf_pos += len;\r
+                       return len;\r
+               }\r
+               for(int i = 0; i < len; i++) {\r
+                       int c = this.read();\r
+                       if (c != -1) {\r
+                               b[off+i] = (byte) c;\r
+                       } else {\r
+                               return i;\r
+                       }\r
+               }\r
+               return len;\r
+       }\r
+       \r
+       @Override\r
+    public long getFilePointer() throws IOException{\r
+               long l = real_pos;\r
+               return (l - buf_end + buf_pos);\r
+       }\r
+\r
+       @Override\r
+    public void seek(long pos) throws IOException {\r
+               int n = (int) (real_pos - pos);\r
+               if(n >= 0 && n <= buf_end) {\r
+                       buf_pos = buf_end - n;\r
+               } else {\r
+                       super.seek(pos);\r
+                       invalidate();\r
+               }\r
+       }\r
+\r
+       public final String getNextLine() throws IOException {\r
+               String str = null;\r
+               if (buf_end - buf_pos <= 0) {\r
+                       if (fillBuffer() < 0) {\r
+                               return null;\r
+                       }\r
+               }\r
+               int lineend = -1;\r
+               for (int i = buf_pos; i < buf_end; i++) {\r
+                       if (buffer[i] == '\n') {\r
+                               lineend = i;\r
+                               break;\r
+                       }\r
+               }\r
+               if (lineend < 0) {\r
+                       sb.delete(0, sb.length());\r
+                       int c;\r
+                       while (((c = read()) != -1) && (c != '\n')) {\r
+                               sb.append((char) c);\r
+                       }\r
+                       if ((c == -1) && (sb.length() == 0)) {\r
+                               return null;\r
+                       }\r
+                       if (sb.charAt(sb.length() - 1) == '\r') {\r
+                               sb.deleteCharAt(sb.length() - 1);\r
+                       }\r
+                       return sb.toString();\r
+               }\r
+               if (lineend > 0 && buffer[lineend - 1] == '\r' && lineend > buf_pos) {\r
+                       str = new String(buffer, buf_pos, lineend - buf_pos - 1, CHARSET_UTF8);\r
+               } else {\r
+                       str = new String(buffer, buf_pos, lineend - buf_pos, CHARSET_UTF8);\r
+               }\r
+               buf_pos = lineend + 1;\r
+               return str;\r
+       }\r
+         \r
+       private int fillBuffer() throws IOException {\r
+               int n = super.read(buffer, 0, BUF_SIZE);\r
+               if (n >= 0) {\r
+                       real_pos += n;\r
+                       buf_end = n;\r
+                       buf_pos = 0;\r
+               }\r
+               return n;\r
+       }\r
+       \r
+       private void invalidate() throws IOException {\r
+               buf_end = 0;\r
+               buf_pos = 0;\r
+               real_pos = super.getFilePointer();\r
+       }\r
+}\r
This page took 0.029366 seconds and 5 git commands to generate.