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;
14 import com.stevesoft.pat.*;
17 * Provides a wrapper for a RandomAccessFile so that it can be searched by
20 public class RandomAccessFileWrap implements StringLike
25 public void setOffset(long o)
31 public long getOffset()
40 byte[] buf = new byte[1024];
42 public int getBufferSize()
47 public void setBufferSize(int bs)
53 public RandomAccessFileWrap(String file) throws IOException
55 this.raf = new RandomAccessFile(file, "r");
58 public RandomAccessFileWrap(RandomAccessFile raf)
63 public char charAt(int i)
65 if (i >= i0 && i < iend)
67 return (char) buf[i - i0];
73 // if(i0+offset<0) i0=(int)(-offset);
78 raf.seek(i0 + offset);
79 iend = i0 + raf.read(buf, 0, buf.length);
81 if (i >= i0 && i < iend)
83 return (char) buf[i - i0];
89 throw new ArrayIndexOutOfBoundsException(MessageManager.formatMessage("exception.out_of_bounds_for_file", new String[]{
90 Integer.valueOf(i).toString(),
91 Integer.valueOf(i0).toString(),
92 Integer.valueOf(iend).toString()
96 public String toString()
98 throw new Error(MessageManager.getString("error.not_implemented"));
105 long len = raf.length() - offset;
106 if (len > Integer.MAX_VALUE)
108 return Integer.MAX_VALUE;
111 } catch (IOException ioe)
117 public String substring(int i1, int i2)
119 StringBuffer sb = new StringBuffer();
120 for (int i = i1; i < i2; i++)
122 sb.append(charAt(i));
124 return sb.toString();
127 public Object unwrap()
132 public static void main(String[] files) throws IOException
134 for (int i = 0; i < files.length; i++)
136 RandomAccessFileWrap fw = new RandomAccessFileWrap(
137 new RandomAccessFile(files[i], "r"));
138 Regex r = new Regex("toString\\(\\) *(?@{})");
141 System.out.print(files[i] + " ");
145 if (r.searchFrom(fw, j))
147 System.out.println("Matched at index: " + r.matchedFrom());
152 System.out.println("not found");
154 System.out.println(r.stringMatched());
155 } while (r.didMatch());
159 public BasicStringBufferLike newStringBufferLike()
161 return new StringBufferWrap();
164 public int indexOf(char c)
166 for (int i = 0; i < length(); i++)