Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 10:11:34 GMT
  2. Package com.stevesoft.pat.wrap

File RandomAccessFileWrap.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

20
49
14
1
190
147
28
0.57
3.5
14
2

Classes

Class Line # Actions
RandomAccessFileWrap 23 49 28
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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