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