JAL-3438 spotless for 2.11.2.0
[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   @Override
67   public char charAt(int i)
68   {
69     if (i >= i0 && i < iend)
70     {
71       return (char) buf[i - i0];
72     }
73
74     try
75     {
76       i0 = i - 5;
77       // if(i0+offset<0) i0=(int)(-offset);
78       if (i0 < 0)
79       {
80         i0 = 0;
81       }
82       raf.seek(i0 + offset);
83       iend = i0 + raf.read(buf, 0, buf.length);
84
85       if (i >= i0 && i < iend)
86       {
87         return (char) buf[i - i0];
88       }
89     } catch (Throwable t)
90     {
91     }
92
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() }));
97   }
98
99   @Override
100   public String toString()
101   {
102     throw new Error(MessageManager.getString("error.not_implemented"));
103   }
104
105   @Override
106   public int length()
107   {
108     try
109     {
110       long len = raf.length() - offset;
111       if (len > Integer.MAX_VALUE)
112       {
113         return Integer.MAX_VALUE;
114       }
115       return (int) len;
116     } catch (IOException ioe)
117     {
118       return 0;
119     }
120   }
121
122   @Override
123   public String substring(int i1, int i2)
124   {
125     StringBuffer sb = new StringBuffer();
126     for (int i = i1; i < i2; i++)
127     {
128       sb.append(charAt(i));
129     }
130     return sb.toString();
131   }
132
133   @Override
134   public Object unwrap()
135   {
136     return raf;
137   }
138
139   /**
140    * @j2sIgnore
141    * 
142    * @param files
143    * @throws IOException
144    */
145   public static void main(String[] files) throws IOException
146   {
147     for (int i = 0; i < files.length; i++)
148     {
149       RandomAccessFileWrap fw = new RandomAccessFileWrap(
150               new RandomAccessFile(files[i], "r"));
151       Regex r = new Regex("toString\\(\\) *(?@{})");
152       r.setGFlag(true);
153       r.optimize();
154       System.out.print(files[i] + " ");
155       int j = 0;
156       do
157       {
158         if (r.searchFrom(fw, j))
159         {
160           System.out.println("Matched at index: " + r.matchedFrom());
161           j = r.matchedTo();
162         }
163         else
164         {
165           System.out.println("not found");
166         }
167         System.out.println(r.stringMatched());
168       } while (r.didMatch());
169     }
170   }
171
172   @Override
173   public BasicStringBufferLike newStringBufferLike()
174   {
175     return new StringBufferWrap();
176   }
177
178   @Override
179   public int indexOf(char c)
180   {
181     for (int i = 0; i < length(); i++)
182     {
183       if (charAt(i) == c)
184       {
185         return i;
186       }
187     }
188     return -1;
189   }
190 }