merge from 2_4_Release branch
[jalview.git] / src / com / stevesoft / pat / wrap / RandomAccessFileWrap.java
1 //
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
5 // the details.
6 //    -- Happy Computing!
7 //
8 package com.stevesoft.pat.wrap;
9
10 import java.io.*;
11
12 import com.stevesoft.pat.*;
13
14 /**
15  * Provides a wrapper for a RandomAccessFile so that it can be searched by
16  * Regex.
17  */
18 public class RandomAccessFileWrap implements StringLike
19 {
20
21   long offset = 0;
22
23   public void setOffset(long o)
24   {
25     offset = o;
26     i0 = iend = 0;
27   }
28
29   public long getOffset()
30   {
31     return offset;
32   }
33
34   RandomAccessFile raf;
35
36   int i0 = 0, iend = 0;
37
38   byte[] buf = new byte[1024];
39
40   public int getBufferSize()
41   {
42     return buf.length;
43   }
44
45   public void setBufferSize(int bs)
46   {
47     buf = new byte[bs];
48     i0 = iend = 0;
49   }
50
51   public RandomAccessFileWrap(String file) throws IOException
52   {
53     this.raf = new RandomAccessFile(file, "r");
54   }
55
56   public RandomAccessFileWrap(RandomAccessFile raf)
57   {
58     this.raf = raf;
59   }
60
61   public char charAt(int i)
62   {
63     if (i >= i0 && i < iend)
64     {
65       return (char) buf[i - i0];
66     }
67
68     try
69     {
70       i0 = i - 5;
71       // if(i0+offset<0) i0=(int)(-offset);
72       if (i0 < 0)
73       {
74         i0 = 0;
75       }
76       raf.seek(i0 + offset);
77       iend = i0 + raf.read(buf, 0, buf.length);
78
79       if (i >= i0 && i < iend)
80       {
81         return (char) buf[i - i0];
82       }
83     } catch (Throwable t)
84     {
85     }
86
87     throw new ArrayIndexOutOfBoundsException("Out of bounds for file:"
88             + " i=" + i + ", Final Buffer: i0=" + i0 + " iend=" + iend);
89   }
90
91   public String toString()
92   {
93     throw new Error("Not implemented");
94   }
95
96   public int length()
97   {
98     try
99     {
100       long len = raf.length() - offset;
101       if (len > Integer.MAX_VALUE)
102       {
103         return Integer.MAX_VALUE;
104       }
105       return (int) len;
106     } catch (IOException ioe)
107     {
108       return 0;
109     }
110   }
111
112   public String substring(int i1, int i2)
113   {
114     StringBuffer sb = new StringBuffer();
115     for (int i = i1; i < i2; i++)
116     {
117       sb.append(charAt(i));
118     }
119     return sb.toString();
120   }
121
122   public Object unwrap()
123   {
124     return raf;
125   }
126
127   public static void main(String[] files) throws IOException
128   {
129     for (int i = 0; i < files.length; i++)
130     {
131       RandomAccessFileWrap fw = new RandomAccessFileWrap(
132               new RandomAccessFile(files[i], "r"));
133       Regex r = new Regex("toString\\(\\) *(?@{})");
134       r.setGFlag(true);
135       r.optimize();
136       System.out.print(files[i] + " ");
137       int j = 0;
138       do
139       {
140         if (r.searchFrom(fw, j))
141         {
142           System.out.println("Matched at index: " + r.matchedFrom());
143           j = r.matchedTo();
144         }
145         else
146         {
147           System.out.println("not found");
148         }
149         System.out.println(r.stringMatched());
150       } while (r.didMatch());
151     }
152   }
153
154   public BasicStringBufferLike newStringBufferLike()
155   {
156     return new StringBufferWrap();
157   }
158
159   public int indexOf(char c)
160   {
161     for (int i = 0; i < length(); i++)
162     {
163       if (charAt(i) == c)
164       {
165         return i;
166       }
167     }
168     return -1;
169   }
170 }