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
94 .formatMessage("exception.out_of_bounds_for_file", new String[]
95 { Integer.valueOf(i).toString(), Integer.valueOf(i0).toString(),
96 Integer.valueOf(iend).toString() }));
100 public String toString()
102 throw new Error(MessageManager.getString("error.not_implemented"));
110 long len = raf.length() - offset;
111 if (len > Integer.MAX_VALUE)
113 return Integer.MAX_VALUE;
116 } catch (IOException ioe)
123 public String substring(int i1, int i2)
125 StringBuffer sb = new StringBuffer();
126 for (int i = i1; i < i2; i++)
128 sb.append(charAt(i));
130 return sb.toString();
134 public Object unwrap()
143 * @throws IOException
145 public static void main(String[] files) throws IOException
147 for (int i = 0; i < files.length; i++)
149 RandomAccessFileWrap fw = new RandomAccessFileWrap(
150 new RandomAccessFile(files[i], "r"));
151 Regex r = new Regex("toString\\(\\) *(?@{})");
154 System.out.print(files[i] + " ");
158 if (r.searchFrom(fw, j))
160 System.out.println("Matched at index: " + r.matchedFrom());
165 System.out.println("not found");
167 System.out.println(r.stringMatched());
168 } while (r.didMatch());
173 public BasicStringBufferLike newStringBufferLike()
175 return new StringBufferWrap();
179 public int indexOf(char c)
181 for (int i = 0; i < length(); i++)