01: /*
02: * @(#)writeChar.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: * writeChar(DataOutput dout, char value)
16: * writeChar(DataOutput dout, char[] src, int offset, int len)
17: */
18: public class writeChar extends PnutsFunction {
19:
20: public writeChar() {
21: super ("writeChar");
22: }
23:
24: public boolean defined(int narg) {
25: return (narg == 2 || narg == 4);
26: }
27:
28: protected Object exec(Object[] args, Context context) {
29: int nargs = args.length;
30: try {
31: if (nargs == 2) {
32: DataOutput dout = (DataOutput) args[0];
33: char value = ((Character) args[1]).charValue();
34: dout.writeChar(value);
35: return new Integer(1);
36: } else if (nargs == 4) {
37: DataOutput dout = (DataOutput) args[0];
38: char[] src = (char[]) args[1];
39: int offset = ((Integer) args[2]).intValue();
40: int n = ((Integer) args[3]).intValue();
41: for (int i = 0; i < n; i++) {
42: dout.writeChar(src[offset + i]);
43: }
44: return new Integer(n);
45: } else {
46: undefined(args, context);
47: return null;
48: }
49: } catch (IOException e) {
50: throw new PnutsException(e, context);
51: }
52: }
53:
54: public String toString() {
55: return "function writeChar(DataOutput dout, {char value | char[] src, int offset, int len})";
56: }
57: }
|