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)
66 public char charAt(int i)
68 if (i >= i0 && i < iend)
70 return (char) buf[i - i0];
76 // if(i0+offset<0) i0=(int)(-offset);
81 raf.seek(i0 + offset);
82 iend = i0 + raf.read(buf, 0, buf.length);
84 if (i >= i0 && i < iend)
86 return (char) buf[i - i0];
92 throw new ArrayIndexOutOfBoundsException(MessageManager.formatMessage(
93 "exception.out_of_bounds_for_file", new String[] {
94 Integer.valueOf(i).toString(),
95 Integer.valueOf(i0).toString(),
96 Integer.valueOf(iend).toString() }));
99 public String toString()
101 throw new Error(MessageManager.getString("error.not_implemented"));
108 long len = raf.length() - offset;
109 if (len > Integer.MAX_VALUE)
111 return Integer.MAX_VALUE;
114 } catch (IOException ioe)
120 public String substring(int i1, int i2)
122 StringBuffer sb = new StringBuffer();
123 for (int i = i1; i < i2; i++)
125 sb.append(charAt(i));
127 return sb.toString();
130 public Object unwrap()
135 public static void main(String[] files) throws IOException
137 for (int i = 0; i < files.length; i++)
139 RandomAccessFileWrap fw = new RandomAccessFileWrap(
140 new RandomAccessFile(files[i], "r"));
141 Regex r = new Regex("toString\\(\\) *(?@{})");
144 System.out.print(files[i] + " ");
148 if (r.searchFrom(fw, j))
150 System.out.println("Matched at index: " + r.matchedFrom());
155 System.out.println("not found");
157 System.out.println(r.stringMatched());
158 } while (r.didMatch());
162 public BasicStringBufferLike newStringBufferLike()
164 return new StringBufferWrap();
167 public int indexOf(char c)
169 for (int i = 0; i < length(); i++)