001: /*
002: * $Id: AnyOutputStream.java,v 1.27 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.Array;
016: import anvil.core.Serialization;
017: import anvil.script.Context;
018: import java.io.IOException;
019: import java.io.OutputStream;
020: import java.util.Enumeration;
021:
022: /// @class OutputStream
023: /// This class represents an output stream of bytes
024:
025: /**
026: * class AnyOutputStream
027: *
028: * @author: Jani Lehtimäki
029: */
030: public class AnyOutputStream extends AnyAbstractClass {
031:
032: private OutputStream _output;
033:
034: public AnyOutputStream(OutputStream output) {
035: _output = output;
036: }
037:
038: public final anvil.script.ClassType classOf() {
039: return __class__;
040: }
041:
042: public Object toObject() {
043: return _output;
044: }
045:
046: /// @method close
047: /// Closes this output stream and releases any system resources associated with
048: /// this stream. A closed stream cannot perform output operations and cannot be
049: /// reopened.
050: /// @synopsis void close()
051: /// @throws IOError If an I/O error occurs.
052: public Any m_close(Context context) {
053: try {
054: _output.close();
055: return TRUE;
056: } catch (IOException e) {
057: throw context.exception(e);
058: }
059: }
060:
061: /// @method flush
062: /// Flushes this output stream and forces any buffered output bytes
063: /// to be written out.
064: /// @synopsis OutputStream flush()
065: /// @throws IOError If an I/O error occurs.
066: public Any m_flush(Context context) {
067: try {
068: _output.flush();
069: return this ;
070: } catch (IOException e) {
071: throw context.exception(e);
072: }
073: }
074:
075: private void doWrite(Any value) throws IOException {
076: int n;
077: switch (value.typeOf()) {
078: case IS_NULL:
079: case IS_UNDEFINED:
080: break;
081:
082: case IS_INT:
083: _output.write(value.toInt());
084: break;
085:
086: case IS_LIST:
087: case IS_TUPLE:
088: Any[] list = value.toTuple();
089: n = value.sizeOf();
090: for (int i = 0; i < n; i++) {
091: doWrite(list[i]);
092: }
093: break;
094:
095: case IS_ARRAY:
096: Enumeration lines = value.toArray().elements();
097: while (lines.hasMoreElements()) {
098: doWrite((Any) lines.nextElement());
099: }
100: break;
101:
102: case IS_BINARY:
103: n = value.sizeOf();
104: if (n > 0) {
105: _output.write(value.toBinary(), 0, n);
106: }
107: break;
108:
109: default:
110: String buffer = value.toString();
111: if (buffer.length() > 0) {
112: _output.write(anvil.util.Conversions.getBytes(buffer));
113: }
114: }
115: }
116:
117: /// @method write
118: /// @synopsis OutputStream write(int byte, ...) ;
119: /// Writes a single byte into stream.
120: /// @synopsis OutputStream write(string text, ...) ;
121: /// Writes string into stream
122: /// @synopsis OutputStream write(binary bin, ...) ;
123: /// Writes bytes into stream
124: /// @throws IOError If an I/O error occurs.
125: public static final Object[] p_write = new Object[] { null,
126: "values" };
127:
128: public Any m_write(Context context, Any[] parameters) {
129: int n = parameters.length;
130: try {
131: for (int i = 0; i < n; i++) {
132: Any value = parameters[i];
133: switch (value.typeOf()) {
134: case IS_NULL:
135: case IS_UNDEFINED:
136: break;
137: case IS_INT:
138: _output.write(value.toInt());
139: break;
140: case IS_BINARY:
141: int len = value.sizeOf();
142: if (n > 0) {
143: _output.write(value.toBinary(), 0, len);
144: }
145: break;
146: default:
147: String buffer = value.toString();
148: if (buffer.length() > 0) {
149: _output.write(anvil.util.Conversions
150: .getBytes(buffer));
151: }
152: }
153: }
154: return this ;
155: } catch (IOException e) {
156: throw context.exception(e);
157: }
158: }
159:
160: /// @method writeBinary
161: /// Writes bytes from specified binary into stream.
162: /// @synopsis OutputStream writeBinary(binary bin)
163: /// @synopsis OutputStream writeBinary(binary bin, int offset, int length)
164: /// @param offset The start offset in the data.
165: /// @param length The number of bytes to write.
166: /// @throws IOError If an I/O error occurs.
167: public static final Object[] p_writeBinary = new Object[] { null,
168: "array", "*offset", null, "*length", null };
169:
170: public Any m_writeBinary(Context context, Any binary, Any offset_,
171: Any length_) {
172: if (!binary.isBinary()) {
173: throw context.BadParameter("First parameter is not binary");
174: }
175: byte[] array = binary.toBinary();
176: int size = array.length;
177: int offset = 0;
178: int length = size;
179: if (offset_ != null) {
180: offset = offset_.toInt();
181: if (length_ != null) {
182: length = length_.toInt();
183: }
184: if (offset < 0) {
185: offset = 0;
186: }
187: if (offset >= size) {
188: return this ;
189: }
190: if (offset + length > size) {
191: length = size - offset;
192: }
193: }
194: try {
195: _output.write(array, offset, length);
196: return this ;
197: } catch (IOException e) {
198: throw context.exception(e);
199: }
200: }
201:
202: /// @method writeData
203: /// Serializes given values into stream.
204: /// @synopsis OutputStream writeData(object data, ...)
205: /// @throws IOError If an I/O error occurs.
206: public static final Object[] p_writeData = new Object[] { null,
207: "values" };
208:
209: public Any m_writeData(Context context, Any[] parameters) {
210: int n = parameters.length;
211: try {
212: for (int i = 0; i < n; i++) {
213: Serialization
214: .serialize(context, parameters[i], _output);
215: }
216: return this ;
217: } catch (IOException e) {
218: throw context.exception(e);
219: }
220: }
221:
222: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
223: "OutputStream",
224: AnyOutputStream.class,
225: //DOC{{
226: ""
227: + " @class OutputStream\n"
228: + " This class represents an output stream of bytes\n"
229: + " @method close\n"
230: + " Closes this output stream and releases any system resources associated with \n"
231: + " this stream. A closed stream cannot perform output operations and cannot be\n"
232: + " reopened. \n"
233: + " @synopsis void close()\n"
234: + " @throws IOError If an I/O error occurs.\n"
235: + " @method flush\n"
236: + " Flushes this output stream and forces any buffered output bytes \n"
237: + " to be written out. \n"
238: + " @synopsis OutputStream flush()\n"
239: + " @throws IOError If an I/O error occurs.\n"
240: + " @method write\n"
241: + " @synopsis OutputStream write(int byte, ...) ;\n"
242: + " Writes a single byte into stream.\n"
243: + " @synopsis OutputStream write(string text, ...) ;\n"
244: + " Writes string into stream\n"
245: + " @synopsis OutputStream write(binary bin, ...) ;\n"
246: + " Writes bytes into stream\n"
247: + " @throws IOError If an I/O error occurs.\n"
248: + " @method writeBinary\n"
249: + " Writes bytes from specified binary into stream.\n"
250: + " @synopsis OutputStream writeBinary(binary bin)\n"
251: + " @synopsis OutputStream writeBinary(binary bin, int offset, int length)\n"
252: + " @param offset The start offset in the data.\n"
253: + " @param length The number of bytes to write.\n"
254: + " @throws IOError If an I/O error occurs.\n"
255: + " @method writeData\n"
256: + " Serializes given values into stream.\n"
257: + " @synopsis OutputStream writeData(object data, ...)\n"
258: + " @throws IOError If an I/O error occurs.\n"
259: //}}DOC
260: );
261: static {
262: IOModule.class.getName();
263: }
264:
265: }
|