001: /*
002: * $Id: AnyInputStream.java,v 1.29 2002/09/16 08:05:02 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.io;
011:
012: import anvil.core.Any;
013: import anvil.core.AnyAbstractClass;
014: import anvil.core.AnyBinary;
015: import anvil.core.AnyString;
016: import anvil.core.AnyList;
017: import anvil.core.Array;
018: import anvil.core.Serialization;
019: import anvil.core.UnserializationException;
020: import anvil.script.Context;
021: import anvil.java.util.BindingEnumeration;
022: import anvil.java.io.GenericInputStream;
023: import java.io.InputStream;
024: import java.io.InterruptedIOException;
025: import java.io.IOException;
026:
027: /// @class InputStream
028: ///
029: /// Datatype representing an input stream of bytes.
030: ///
031: /// @operator "enumeration *<b>this</b>"
032: /// Returns enumeration (of strings) that will read the stream
033: /// line-by-line.
034: ///
035: public class AnyInputStream extends AnyAbstractClass {
036:
037: private GenericInputStream _input;
038:
039: public AnyInputStream(InputStream input) {
040: _input = new GenericInputStream(input);
041: }
042:
043: public final anvil.script.ClassType classOf() {
044: return __class__;
045: }
046:
047: public Object toObject() {
048: return _input;
049: }
050:
051: public BindingEnumeration enumeration() {
052: return new InputStreamEnumeration(_input);
053: }
054:
055: /// @method available
056: /// Returns number of bytes available from this stream
057: /// without blocking.
058: /// @synopsis int available()
059: /// @throws IOError If an I/O error occurs.
060: public Any m_available(Context context) {
061: try {
062: return Any.create(_input.available());
063: } catch (IOException e) {
064: throw context.exception(e);
065: }
066: }
067:
068: /// @method close
069: /// Closes this input stream and releases any system resources associated with the stream.
070: /// @synopsis void close()
071: /// @throws IOError If an I/O error occurs.
072: public Any m_close(Context context) {
073: try {
074: _input.close();
075: return this ;
076: } catch (IOException e) {
077: throw context.exception(e);
078: }
079: }
080:
081: /// @method read
082: /// Reads bytes from stream.
083: /// @synopsis int read() ; Reads a single byte from stream.
084: /// @synopsis string read(int maxBytes) ; Reads upto given
085: /// amount of bytes from stream.
086: /// @return Byte read, or bytes read as string, or <code>null</code>
087: /// if end of stream has been reached.
088: /// @throws IOError If an I/O error occurs.
089: public static final Object[] p_read = new Object[] { null,
090: "*maxBytes", null };
091:
092: public Any m_read(Context context, Any max) {
093: try {
094: if (max == null) {
095: int ch = _input.read();
096: if (ch == -1) {
097: return NULL;
098: } else {
099: return Any.create((char) ch);
100: }
101:
102: } else {
103: int amount = max.toInt();
104: if (amount > 1) {
105: return Any.create(_input.read(amount));
106: } else {
107: return NULL;
108: }
109: }
110: } catch (IOException e) {
111: throw context.exception(e);
112: }
113: }
114:
115: /// @method readBinary
116: /// Reads bytes from stream.
117: /// @synopsis int readBinary(binary bin)
118: /// @synopsis int readBinary(binary bin, int offset, int length)
119: /// @param array The binary into which the data is written
120: /// @param offset The start offset in binary
121: /// @param length The maximum number of bytes to read.
122: /// @return Number of bytes read, or -1 if end of stream has been
123: /// reached.
124: /// @throws IOError If an I/O error occurs.
125: public static final Object[] p_readBinary = new Object[] { null,
126: "array", "*offset", null, "*length", null };
127:
128: public Any m_readBinary(Context context, Any binary, Any offset_,
129: Any length_) {
130: if (!binary.isBinary()) {
131: throw context.BadParameter("First parameter it not binary");
132: }
133: byte[] array = binary.toBinary();
134: try {
135: int size = array.length;
136: int offset = 0;
137: int length = size;
138: if (offset_ != null) {
139: offset = offset_.toInt();
140: if (length_ != null) {
141: length = length_.toInt();
142: }
143: if (offset < 0) {
144: offset = 0;
145: }
146: if (offset >= size) {
147: return ZERO;
148: }
149: if (offset + length > size) {
150: length = size - offset;
151: }
152: }
153: return Any.create(_input.read(array, offset, length));
154: } catch (IOException e) {
155: throw context.exception(e);
156: }
157: }
158:
159: /// @method readLine
160: /// Reads single line from stream.
161: /// @synopsis string readLine()
162: /// @return Line read or, <code>null</code> if end of stream was
163: /// reached.
164: /// @throws IOError If an I/O error occurs.
165: public Any m_readLine(Context context) {
166: try {
167: String buffer = _input.readLine();
168: return (buffer == null) ? NULL : new AnyString(buffer);
169: } catch (IOException e) {
170: throw context.exception(e);
171: }
172: }
173:
174: /// @method readLines
175: /// @synopsis list readLines() ;
176: /// Reads all available lines from stream
177: /// @synopsis list readLines(int maxLines) ;
178: /// Reads upto given amount of lines from stream.
179: /// @param maxLinex Maximum number of lines to return
180: /// @return Lines read as list of strings with linefeeds
181: /// stripped.
182: /// @throws IOError If an I/O error occurs.
183: public static final Object[] p_readLines = new Object[] { null,
184: "*maxLines", new Integer(0) };
185:
186: public Any m_readLines(Context context, int max) {
187: try {
188: String buffer;
189: if (max > 0) {
190: Any[] list = new Any[max];
191: int index = 0;
192: while ((max > 0)
193: && (buffer = _input.readLine()) != null) {
194: list[index] = Any.create(buffer);
195: max--;
196: index++;
197: }
198: return new AnyList(list, index);
199: } else {
200: AnyList lines = new AnyList(new Any[8], 0);
201: while ((buffer = _input.readLine()) != null) {
202: lines.append(Any.create(buffer));
203: }
204: return lines;
205: }
206: } catch (IOException e) {
207: throw context.exception(e);
208: }
209: }
210:
211: /// @method readData
212: /// Reads serialized data from this stream.
213: /// @see anvil.lang.OutputStream.write
214: /// @synopsis object readData()
215: /// @return Unserialized data
216: /// @throws IOError If an I/O error occurs.
217: /// @throws CorruptedSerialization If serialized data is corrupted
218: public Any m_readData(Context context) {
219: try {
220: return Serialization.unserialize(context, _input);
221: } catch (IOException e) {
222: throw context.exception(e);
223: } catch (UnserializationException e) {
224: throw context.CorruptedSerialization();
225: }
226: }
227:
228: /// @method getColumnNumber
229: /// Gets the current column number of this stream.
230: /// Useful only with text content.
231: /// @synopsis int getColumnNumber()
232: public Any m_getColumnNumber() {
233: return Any.create(_input.getColumnNumber());
234: }
235:
236: /// @method getLineNumber
237: /// Gets the current line number of this stream.
238: /// Useful only with text content.
239: /// @synopsis int getLineNumber()
240: public Any m_getLineNumber() {
241: return Any.create(_input.getLineNumber());
242: }
243:
244: /// @method skip
245: /// Skips over some bytes in this stream.
246: /// @synopsis int skip(int amount)
247: /// @param amount Amount of bytes to skip
248: /// @return Amount of bytes skipped, or -1 if end of stream was reached
249: /// before any bytes were skipped.
250: /// @throws IOError If an I/O error occurs.
251: public static final Object[] p_skip = new Object[] { null, "amount" };
252:
253: public Any m_skip(Context context, int amount) {
254: try {
255: return Any.create(_input.skip(amount));
256: } catch (IOException e) {
257: throw context.exception(e);
258: }
259: }
260:
261: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
262: "InputStream",
263: AnyInputStream.class,
264: //DOC{{
265: ""
266: + " @class InputStream\n"
267: + "\n"
268: + " Datatype representing an input stream of bytes.\n"
269: + "\n"
270: + " @operator \"enumeration *<b>this</b>\"\n"
271: + " Returns enumeration (of strings) that will read the stream\n"
272: + " line-by-line.\n"
273: + "\n"
274: + " @method available\n"
275: + " Returns number of bytes available from this stream\n"
276: + " without blocking.\n"
277: + " @synopsis int available()\n"
278: + " @throws IOError If an I/O error occurs.\n"
279: + " @method close\n"
280: + " Closes this input stream and releases any system resources associated with the stream. \n"
281: + " @synopsis void close()\n"
282: + " @throws IOError If an I/O error occurs.\n"
283: + " @method read\n"
284: + " Reads bytes from stream.\n"
285: + " @synopsis int read() ; Reads a single byte from stream.\n"
286: + " @synopsis string read(int maxBytes) ; Reads upto given\n"
287: + " amount of bytes from stream.\n"
288: + " @return Byte read, or bytes read as string, or <code>null</code>\n"
289: + " if end of stream has been reached.\n"
290: + " @throws IOError If an I/O error occurs.\n"
291: + " @method readBinary\n"
292: + " Reads bytes from stream.\n"
293: + " @synopsis int readBinary(binary bin)\n"
294: + " @synopsis int readBinary(binary bin, int offset, int length)\n"
295: + " @param array The binary into which the data is written\n"
296: + " @param offset The start offset in binary\n"
297: + " @param length The maximum number of bytes to read.\n"
298: + " @return Number of bytes read, or -1 if end of stream has been\n"
299: + " reached.\n"
300: + " @throws IOError If an I/O error occurs.\n"
301: + " @method readLine\n"
302: + " Reads single line from stream.\n"
303: + " @synopsis string readLine()\n"
304: + " @return Line read or, <code>null</code> if end of stream was\n"
305: + " reached.\n"
306: + " @throws IOError If an I/O error occurs.\n"
307: + " @method readLines\n"
308: + " @synopsis list readLines() ; \n"
309: + " Reads all available lines from stream\n"
310: + " @synopsis list readLines(int maxLines) ; \n"
311: + " Reads upto given amount of lines from stream. \n"
312: + " @param maxLinex Maximum number of lines to return\n"
313: + " @return Lines read as list of strings with linefeeds\n"
314: + " stripped.\n"
315: + " @throws IOError If an I/O error occurs.\n"
316: + " @method readData\n"
317: + " Reads serialized data from this stream.\n"
318: + " @see anvil.lang.OutputStream.write\n"
319: + " @synopsis object readData()\n"
320: + " @return Unserialized data\n"
321: + " @throws IOError If an I/O error occurs.\n"
322: + " @throws CorruptedSerialization If serialized data is corrupted\n"
323: + " @method getColumnNumber\n"
324: + " Gets the current column number of this stream. \n"
325: + " Useful only with text content.\n"
326: + " @synopsis int getColumnNumber()\n"
327: + " @method getLineNumber\n"
328: + " Gets the current line number of this stream. \n"
329: + " Useful only with text content.\n"
330: + " @synopsis int getLineNumber()\n"
331: + " @method skip\n"
332: + " Skips over some bytes in this stream.\n"
333: + " @synopsis int skip(int amount)\n"
334: + " @param amount Amount of bytes to skip\n"
335: + " @return Amount of bytes skipped, or -1 if end of stream was reached\n"
336: + " before any bytes were skipped.\n"
337: + " @throws IOError If an I/O error occurs.\n"
338: //}}DOC
339: );
340: static {
341: IOModule.class.getName();
342: }
343:
344: }
|