View Javadoc

1   /* InterruptibleCharSequence
2    * 
3    * Created on Jun 27, 2007
4    *
5    * Copyright (C) 2007 Internet Archive.
6    * 
7    * This file is part of the Heritrix web crawler (crawler.archive.org).
8    * 
9    * Heritrix is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser Public License as published by
11   * the Free Software Foundation; either version 2.1 of the License, or
12   * any later version.
13   * 
14   * Heritrix is distributed in the hope that it will be useful, 
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU Lesser Public License
20   * along with Heritrix; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22   */
23  package org.archive.util;
24  
25  /***
26   * CharSequence that noticed thread interrupts -- as might be necessary 
27   * to recover from a loose regex on unexpected challenging input. 
28   * 
29   * @author gojomo
30   */
31  public class InterruptibleCharSequence implements CharSequence {
32      CharSequence inner;
33      // public long counter = 0; 
34      
35      public InterruptibleCharSequence(CharSequence inner) {
36          super();
37          this.inner = inner;
38      }
39  
40      public char charAt(int index) {
41          if (Thread.interrupted()) { // clears flag if set
42              throw new RuntimeException(new InterruptedException());
43          }
44          // counter++;
45          return inner.charAt(index);
46      }
47  
48      public int length() {
49          return inner.length();
50      }
51  
52      public CharSequence subSequence(int start, int end) {
53          return new InterruptibleCharSequence(inner.subSequence(start, end));
54      }
55  
56      @Override
57      public String toString() {
58          return inner.toString();
59      }
60  }