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