View Javadoc
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: PushbackByteSource.java,v 1.5 2003/02/17 04:17:27 michaelh Exp $ 20 */ 21 22 package juju.reattore.io.impl; 23 24 import java.io.IOException; 25 26 import juju.reattore.io.*; 27 28 /*** Simple one character pushback system that works across multiple 29 input arrays. 30 */ 31 public class PushbackByteSource 32 implements ByteSource { 33 34 private int last = EOF; 35 private ByteSource on; 36 37 /*** Create a new source to be seed()ed later on. 38 */ 39 public PushbackByteSource() { 40 } 41 42 /*** Create a new source around the given source. 43 44 @param on The source to wrap. 45 */ 46 public PushbackByteSource(ByteSource on) { 47 this(); 48 seed(on); 49 } 50 51 /*** Seeds the push back system with a new array to read from. 52 Does not discard any existing push back. 53 54 @param on The source to read from. 55 */ 56 public void seed(ByteSource on) { 57 this.on = on; 58 } 59 60 /*** Called when the user is finished with this source. The source 61 is invalid after this call. 62 */ 63 public void dispose() { 64 on.dispose(); 65 } 66 67 /*** @see Source */ 68 public int get() 69 throws IOException { 70 71 if (last != EOF) { 72 int ret = last; 73 last = EOF; 74 75 return ret; 76 } 77 else if (on == null) { 78 return EOF; 79 } 80 else { 81 return on.get(); 82 } 83 } 84 85 /*** @see Source */ 86 public int get(byte[] into, int offset, int length) 87 throws IOException { 88 89 if (last != EOF) { 90 into[offset++] = (byte)last; 91 last = EOF; 92 length--; 93 94 return on.get(into, offset, length) + 1; 95 } 96 else { 97 return on.get(into, offset, length); 98 } 99 } 100 101 /*** @see Source */ 102 public int remaining() 103 throws IOException { 104 105 return on.remaining() + (last != EOF ? 1 : 0); 106 } 107 108 /*** Returns the given byte back to the input. The next call to 109 get() will return this. 110 111 @param b Any byte. Does not have to have been read. 112 */ 113 public void pushback(int b) { 114 assert last == EOF; 115 last = b; 116 } 117 }

This page was automatically generated by Maven