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;
10 import jalview.util.MessageManager;
12 import java.io.IOException;
13 import java.io.RandomAccessFile;
15 import com.stevesoft.pat.BasicStringBufferLike;
16 import com.stevesoft.pat.Regex;
17 import com.stevesoft.pat.StringLike;
20 * Provides a wrapper for a RandomAccessFile so that it can be searched by
23 public class RandomAccessFileWrap implements StringLike
28 public void setOffset(long o)
34 public long getOffset()
43 byte[] buf = new byte[1024];
45 public int getBufferSize()
50 public void setBufferSize(int bs)
56 public RandomAccessFileWrap(String file) throws IOException
58 this.raf = new RandomAccessFile(file, "r");
61 public RandomAccessFileWrap(RandomAccessFile raf)
67 public char charAt(int i)
69 if (i >= i0 && i < iend)
71 return (char) buf[i - i0];
77 // if(i0+offset<0) i0=(int)(-offset);
82 raf.seek(i0 + offset);
83 iend = i0 + raf.read(buf, 0, buf.length);
85 if (i >= i0 && i < iend)
87 return (char) buf[i - i0];
93 throw new ArrayIndexOutOfBoundsException(MessageManager.formatMessage(
94 "exception.out_of_bounds_for_file", new String[] {
95 Integer.valueOf(i).toString(),
96 Integer.valueOf(i0).toString(),
97 Integer.valueOf(iend).toString() }));
101 public String toString()
103 throw new Error(MessageManager.getString("error.not_implemented"));
111 long len = raf.length() - offset;
112 if (len > Integer.MAX_VALUE)
114 return Integer.MAX_VALUE;
117 } catch (IOException ioe)
124 public String substring(int i1, int i2)
126 StringBuffer sb = new StringBuffer();
127 for (int i = i1; i < i2; i++)
129 sb.append(charAt(i));
131 return sb.toString();
135 public Object unwrap()
144 * @throws IOException
146 public static void main(String[] files) throws IOException
148 for (int i = 0; i < files.length; i++)
150 RandomAccessFileWrap fw = new RandomAccessFileWrap(
151 new RandomAccessFile(files[i], "r"));
152 Regex r = new Regex("toString\\(\\) *(?@{})");
155 System.out.print(files[i] + " ");
159 if (r.searchFrom(fw, j))
161 System.out.println("Matched at index: " + r.matchedFrom());
166 System.out.println("not found");
168 System.out.println(r.stringMatched());
169 } while (r.didMatch());
174 public BasicStringBufferLike newStringBufferLike()
176 return new StringBufferWrap();
180 public int indexOf(char c)
182 for (int i = 0; i < length(); i++)