home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1995 November
/
PCWK1195.iso
/
inne
/
win95
/
sieciowe
/
hotja32.lzh
/
hotjava
/
classsrc
/
java
/
io
/
inputstreamstringbuffer.java
< prev
next >
Wrap
Text File
|
1995-08-11
|
3KB
|
126 lines
/*
* @(#)InputStreamStringBuffer.java 1.5 95/03/13 Jonathan Payne
*
* Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package java.io;
/**
* This class implements a String buffer that can be
* used as an InputStream.
* @version 1.11, 31 Jan 1995
* @author Arthur van Hoff
*/
public
class InputStreamStringBuffer extends InputStream {
/**
* The buffer.
*/
String buffer;
/**
* The position in the buffer.
*/
protected int pos;
/**
* The number of characters to use in the buffer.
*/
protected int count;
/**
* Creates an InputStreamStringBuffer, given an array of bytes.
* @param buf the input buffer (not copied)
*/
public InputStreamStringBuffer(String s) {
this.buffer = s;
count = s.length();
}
/**
* Reads a byte.
* @return the byte read, or -1 if the end of the
* stream is reached.
*/
public synchronized int read() {
return (pos < count) ? buffer.charAt(pos++) : -1;
}
/**
* Reads into an array of bytes.
* For efficiency, this method should be overridden in a
* subclass (the default implementation reads 1 byte
* at a time).
* @param b the buffer into which the data is read
* @param off the start offset of the data
* @param len the maximum number of bytes read
* @return the actual number of bytes read; -1 is
* returned when the end of the stream is reached.
*/
public synchronized int read(byte b[], int off, int len) {
if (pos >= count) {
return -1;
}
if (pos + len > count) {
len = count - pos;
}
if (len <= 0) {
return 0;
}
String s = buffer;
int cnt = len;
while (--cnt >= 0) {
b[off++] = (byte)s.charAt(pos++);
}
return len;
}
/**
* Skips bytes of input.
* @param n bytes to be skipped
* @return actual number of bytes skipped
*/
public synchronized int skip(int n) {
if (pos + n > count) {
n = count - pos;
}
if (n < 0) {
return 0;
}
pos += n;
return n;
}
/**
* Returns the number of available bytes in the buffer.
* @return the number of available bytes
*/
public synchronized int available() {
return count - pos;
}
/**
* Resets the buffer to the beginning.
*/
public synchronized void reset() {
pos = 0;
}
}