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