2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat.wrap;
\r
12 import com.stevesoft.pat.*;
\r
14 /** Provides a wrapper for a RandomAccessFile so that it
\r
15 can be searched by Regex. */
\r
16 public class RandomAccessFileWrap
\r
17 implements StringLike
\r
21 public void setOffset(long o)
\r
27 public long getOffset()
\r
32 RandomAccessFile raf;
\r
33 int i0 = 0, iend = 0;
\r
34 byte[] buf = new byte[1024];
\r
36 public int getBufferSize()
\r
41 public void setBufferSize(int bs)
\r
47 public RandomAccessFileWrap(String file)
\r
50 this.raf = new RandomAccessFile(file, "r");
\r
53 public RandomAccessFileWrap(RandomAccessFile raf)
\r
58 public char charAt(int i)
\r
60 if (i >= i0 && i < iend)
\r
62 return (char) buf[i - i0];
\r
68 //if(i0+offset<0) i0=(int)(-offset);
\r
73 raf.seek(i0 + offset);
\r
74 iend = i0 + raf.read(buf, 0, buf.length);
\r
76 if (i >= i0 && i < iend)
\r
78 return (char) buf[i - i0];
\r
84 throw new ArrayIndexOutOfBoundsException("Out of bounds for file:" +
\r
86 ", Final Buffer: i0=" + i0 +
\r
90 public String toString()
\r
92 throw new Error("Not implemented");
\r
99 long len = raf.length() - offset;
\r
100 if (len > Integer.MAX_VALUE)
\r
102 return Integer.MAX_VALUE;
\r
106 catch (IOException ioe)
\r
112 public String substring(int i1, int i2)
\r
114 StringBuffer sb = new StringBuffer();
\r
115 for (int i = i1; i < i2; i++)
\r
117 sb.append(charAt(i));
\r
119 return sb.toString();
\r
122 public Object unwrap()
\r
127 public static void main(String[] files)
\r
130 for (int i = 0; i < files.length; i++)
\r
132 RandomAccessFileWrap fw =
\r
133 new RandomAccessFileWrap(new RandomAccessFile(files[i], "r"));
\r
134 Regex r = new Regex("toString\\(\\) *(?@{})");
\r
137 System.out.print(files[i] + " ");
\r
141 if (r.searchFrom(fw, j))
\r
143 System.out.println("Matched at index: " +
\r
149 System.out.println("not found");
\r
151 System.out.println(r.stringMatched());
\r
153 while (r.didMatch());
\r
157 public BasicStringBufferLike newStringBufferLike()
\r
159 return new StringBufferWrap();
\r
162 public int indexOf(char c)
\r
164 for (int i = 0; i < length(); i++)
\r
166 if (charAt(i) == c)
\r