01: /*
02: * @(#)readBytes.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: * readBytes(InputStream out, byte[] b)
16: * readBytes(InputStream out, byte[] b, int offset, int len)
17: */
18: public class readBytes extends PnutsFunction {
19:
20: public readBytes() {
21: super ("readBytes");
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: InputStream in = (InputStream) 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: int n = in.read(b, offset, size);
47: return new Integer(n);
48: } catch (IOException e) {
49: throw new PnutsException(e, context);
50: }
51: }
52:
53: public String toString() {
54: return "function readBytes(InputStream out, byte[] b {, int offset, int len} )";
55: }
56: }
|