2 // This software is now distributed according to
3 // the Lesser Gnu Public License. Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
8 package com.stevesoft.pat.wrap;
12 import com.stevesoft.pat.*;
15 * Provides a wrapper for a RandomAccessFile so that it can be searched by
18 public class RandomAccessFileWrap implements StringLike
23 public void setOffset(long o)
29 public long getOffset()
38 byte[] buf = new byte[1024];
40 public int getBufferSize()
45 public void setBufferSize(int bs)
51 public RandomAccessFileWrap(String file) throws IOException
53 this.raf = new RandomAccessFile(file, "r");
56 public RandomAccessFileWrap(RandomAccessFile raf)
61 public char charAt(int i)
63 if (i >= i0 && i < iend)
65 return (char) buf[i - i0];
71 // if(i0+offset<0) i0=(int)(-offset);
76 raf.seek(i0 + offset);
77 iend = i0 + raf.read(buf, 0, buf.length);
79 if (i >= i0 && i < iend)
81 return (char) buf[i - i0];
87 throw new ArrayIndexOutOfBoundsException("Out of bounds for file:"
88 + " i=" + i + ", Final Buffer: i0=" + i0 + " iend=" + iend);
91 public String toString()
93 throw new Error("Not implemented");
100 long len = raf.length() - offset;
101 if (len > Integer.MAX_VALUE)
103 return Integer.MAX_VALUE;
106 } catch (IOException ioe)
112 public String substring(int i1, int i2)
114 StringBuffer sb = new StringBuffer();
115 for (int i = i1; i < i2; i++)
117 sb.append(charAt(i));
119 return sb.toString();
122 public Object unwrap()
127 public static void main(String[] files) throws IOException
129 for (int i = 0; i < files.length; i++)
131 RandomAccessFileWrap fw = new RandomAccessFileWrap(
132 new RandomAccessFile(files[i], "r"));
133 Regex r = new Regex("toString\\(\\) *(?@{})");
136 System.out.print(files[i] + " ");
140 if (r.searchFrom(fw, j))
142 System.out.println("Matched at index: " + r.matchedFrom());
147 System.out.println("not found");
149 System.out.println(r.stringMatched());
150 } while (r.didMatch());
154 public BasicStringBufferLike newStringBufferLike()
156 return new StringBufferWrap();
159 public int indexOf(char c)
161 for (int i = 0; i < length(); i++)