01: /*
02: * @(#)writeBytes.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.io;
10:
11: import pnuts.lang.*;
12: import java.io.*;
13:
14: /*
15: * writeBytes(OutputStream out, byte[] b)
16: * writeBytes(OutputStream out, byte[] b, int offset, int len)
17: */
18: public class writeBytes extends PnutsFunction {
19:
20: public writeBytes() {
21: super ("writeBytes");
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 2 || nargs == 4;
26: }
27:
28: protected Object exec(Object[] args, Context context) {
29: int nargs = args.length;
30: if (nargs != 2 && nargs != 4) {
31: undefined(args, context);
32: return null;
33: }
34: OutputStream out = (OutputStream) args[0];
35: byte[] b = (byte[]) args[1];
36:
37: int offset, size;
38: if (nargs == 4) {
39: offset = ((Integer) args[2]).intValue();
40: size = ((Integer) args[3]).intValue();
41: } else {
42: offset = 0;
43: size = b.length;
44: }
45: try {
46: out.write(b, offset, size);
47: out.flush();
48: return null;
49: } catch (IOException e) {
50: throw new PnutsException(e, context);
51: }
52: }
53:
54: public String toString() {
55: return "function writeBytes(OutputStream out, byte[] b {, int offset, int len})";
56: }
57: }
|