1 | /* |
---|
2 | * ByteArrayOutputStream.java |
---|
3 | * |
---|
4 | * Copyright (C) 2009 Alessio Stalla |
---|
5 | * $Id: StringOutputStream.java 11434 2008-12-07 23:24:31Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | public final class ByteArrayOutputStream extends Stream |
---|
37 | { |
---|
38 | private final java.io.ByteArrayOutputStream byteArrayOutputStream; |
---|
39 | |
---|
40 | public ByteArrayOutputStream() |
---|
41 | { |
---|
42 | this(UNSIGNED_BYTE_8); //Declared in Stream.java |
---|
43 | } |
---|
44 | |
---|
45 | private ByteArrayOutputStream(LispObject elementType) |
---|
46 | { |
---|
47 | this.elementType = elementType; |
---|
48 | initAsBinaryOutputStream(byteArrayOutputStream = new java.io.ByteArrayOutputStream()); |
---|
49 | } |
---|
50 | |
---|
51 | @Override |
---|
52 | public LispObject typeOf() |
---|
53 | { |
---|
54 | return Symbol.STREAM; //TODO |
---|
55 | } |
---|
56 | |
---|
57 | @Override |
---|
58 | public LispObject classOf() |
---|
59 | { |
---|
60 | return BuiltInClass.STREAM; //TODO |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | public LispObject typep(LispObject type) |
---|
65 | { |
---|
66 | return super.typep(type); //TODO |
---|
67 | } |
---|
68 | |
---|
69 | @Override |
---|
70 | protected long _getFilePosition() |
---|
71 | { |
---|
72 | if (elementType == NIL) |
---|
73 | return 0; |
---|
74 | return byteArrayOutputStream.size(); |
---|
75 | } |
---|
76 | |
---|
77 | public byte[] getByteArray() |
---|
78 | { |
---|
79 | if (elementType == NIL) { |
---|
80 | return new byte[0]; |
---|
81 | } else { |
---|
82 | return byteArrayOutputStream.toByteArray(); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | @Override |
---|
87 | public String toString() |
---|
88 | { |
---|
89 | return unreadableString("BYTE-ARRAY-OUTPUT-STREAM"); |
---|
90 | } |
---|
91 | |
---|
92 | // ### %make-byte-array-output-stream |
---|
93 | // %make-byte-array-output-stream &optional element-type => byte-array-output-stream |
---|
94 | private static final Primitive MAKE_BYTE_ARRAY_OUTPUT_STREAM = |
---|
95 | new Primitive("%make-byte-array-output-stream", PACKAGE_SYS, false, |
---|
96 | "&optional element-type") |
---|
97 | { |
---|
98 | |
---|
99 | @Override |
---|
100 | public LispObject execute() { |
---|
101 | return new ByteArrayOutputStream(); |
---|
102 | } |
---|
103 | |
---|
104 | @Override |
---|
105 | public LispObject execute(LispObject arg) |
---|
106 | { |
---|
107 | return new ByteArrayOutputStream(arg); |
---|
108 | } |
---|
109 | }; |
---|
110 | |
---|
111 | // ### %get-output-stream-bytes |
---|
112 | // %get-output-stream-bytes byte-array-output-stream => java-byte-array |
---|
113 | private static final Primitive GET_OUTPUT_STREAM_STRING = |
---|
114 | new Primitive("%get-output-stream-bytes", PACKAGE_SYS, false, |
---|
115 | "byte-array-output-stream") |
---|
116 | { |
---|
117 | @Override |
---|
118 | public LispObject execute(LispObject arg) |
---|
119 | { |
---|
120 | if (arg instanceof ByteArrayOutputStream) { |
---|
121 | return JavaObject.getInstance(((ByteArrayOutputStream)arg).getByteArray()); |
---|
122 | } |
---|
123 | return error(new TypeError(this, Symbol.STREAM)); //TODO |
---|
124 | } |
---|
125 | }; |
---|
126 | } |
---|