Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: juju/reattore/util/PushbackArray.java


1   /*  Reattore HTTP Server
2   
3       Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      $Id: PushbackArray.java,v 1.4 2003/03/03 05:14:39 michaelh Exp $
20  */
21  package juju.reattore.util;
22  
23  
24  /** Simple one character pushback system that works across multiple
25      input arrays.
26  */
27  public class PushbackArray {
28      /** Returned when all bytes have been read. */
29      public static final int EOF = -256;
30      private int last = EOF;
31      private byte[] on;
32      private int pos;
33  
34      /** Seeds the push back system with a new array to read from.
35          Does not discard any existing push back.
36  
37          @param ab       The array to read from.
38      */
39      public void seed(byte[] ab) {
40          on = ab;
41          pos = 0;
42      }
43  
44      /** Returns the next byte in the pushback or input, or EOF if
45          everything has been read.
46  
47          @return The next byte in the stream.
48      */
49      public int get() {
50          if (last != EOF) {
51              int ret = last;
52              last = EOF;
53  
54              return ret;
55          }
56          else {
57              if (on == null) {
58                  return EOF;
59              }
60              else if (pos < on.length) {
61                  return on[pos++];
62              }
63              else {
64                  return EOF;
65              }
66          }
67      }
68  
69      /** Returns the given byte back to the input.  The next call to
70          get() will return this.
71  
72          @param b        Any byte.  Does not have to have been read.
73      */
74      public void pushback(int b) {
75          assert last == EOF;
76          last = b;
77      }
78  }