1 | /* |
---|
2 | * ByteArrayOutputStream.java |
---|
3 | * |
---|
4 | * Copyright (C) 2009 Alessio Stalla |
---|
5 | * $Id: ByteArrayOutputStream.java 13442 2011-08-05 21:51:28Z 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 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public final class ByteArrayOutputStream extends Stream |
---|
39 | { |
---|
40 | private final java.io.ByteArrayOutputStream byteArrayOutputStream; |
---|
41 | |
---|
42 | public ByteArrayOutputStream() |
---|
43 | { |
---|
44 | this(UNSIGNED_BYTE_8); //Declared in Stream.java |
---|
45 | } |
---|
46 | |
---|
47 | ByteArrayOutputStream(LispObject elementType) |
---|
48 | { |
---|
49 | super(Symbol.SYSTEM_STREAM); |
---|
50 | this.elementType = elementType; |
---|
51 | initAsBinaryOutputStream(byteArrayOutputStream = new java.io.ByteArrayOutputStream(2048)); |
---|
52 | // based on statistics of ABCL's own .cls files |
---|
53 | // as per 20100111, 2048 is the 70th percentile, |
---|
54 | // meaning that only 30% of all .cls files is bigger |
---|
55 | |
---|
56 | // However, *every* .cls file is bigger than 32 bytes; |
---|
57 | // we want to prevent buffer resizing |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public LispObject typeOf() |
---|
62 | { |
---|
63 | return Symbol.STREAM; //TODO |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public LispObject classOf() |
---|
68 | { |
---|
69 | return BuiltInClass.STREAM; //TODO |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public LispObject typep(LispObject type) |
---|
74 | { |
---|
75 | return super.typep(type); //TODO |
---|
76 | } |
---|
77 | |
---|
78 | @Override |
---|
79 | protected long _getFilePosition() |
---|
80 | { |
---|
81 | if (elementType == NIL) |
---|
82 | return 0; |
---|
83 | return byteArrayOutputStream.size(); |
---|
84 | } |
---|
85 | |
---|
86 | public byte[] getByteArray() |
---|
87 | { |
---|
88 | if (elementType == NIL) { |
---|
89 | return new byte[0]; |
---|
90 | } else { |
---|
91 | return byteArrayOutputStream.toByteArray(); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | // ### %make-byte-array-output-stream |
---|
96 | // %make-byte-array-output-stream &optional element-type => byte-array-output-stream |
---|
97 | private static final Primitive MAKE_BYTE_ARRAY_OUTPUT_STREAM = |
---|
98 | new Primitive("%make-byte-array-output-stream", PACKAGE_SYS, false, |
---|
99 | "&optional element-type") |
---|
100 | { |
---|
101 | |
---|
102 | @Override |
---|
103 | public LispObject execute() { |
---|
104 | return new ByteArrayOutputStream(); |
---|
105 | } |
---|
106 | |
---|
107 | @Override |
---|
108 | public LispObject execute(LispObject arg) |
---|
109 | { |
---|
110 | return new ByteArrayOutputStream(arg); |
---|
111 | } |
---|
112 | }; |
---|
113 | |
---|
114 | // ### %get-output-stream-bytes |
---|
115 | // %get-output-stream-bytes byte-array-output-stream => java-byte-array |
---|
116 | private static final Primitive GET_OUTPUT_STREAM_STRING = |
---|
117 | new Primitive("%get-output-stream-bytes", PACKAGE_SYS, false, |
---|
118 | "byte-array-output-stream") |
---|
119 | { |
---|
120 | @Override |
---|
121 | public LispObject execute(LispObject arg) |
---|
122 | { |
---|
123 | if (arg instanceof ByteArrayOutputStream) { |
---|
124 | return JavaObject.getInstance(((ByteArrayOutputStream)arg).getByteArray()); |
---|
125 | } |
---|
126 | return error(new TypeError(this, Symbol.STREAM)); //TODO |
---|
127 | } |
---|
128 | }; |
---|
129 | |
---|
130 | private static final Primitive GET_OUTPUT_STREAM_ARRAY = |
---|
131 | new Primitive("%get-output-stream-array", PACKAGE_SYS, false, |
---|
132 | "byte-array-output-stream") |
---|
133 | { |
---|
134 | @Override |
---|
135 | public LispObject execute(LispObject arg) |
---|
136 | { |
---|
137 | if (arg instanceof ByteArrayOutputStream) |
---|
138 | return new BasicVector_UnsignedByte8(((ByteArrayOutputStream)arg).getByteArray()); |
---|
139 | |
---|
140 | return error(new TypeError(this, Symbol.STREAM)); // TODO |
---|
141 | } |
---|
142 | }; |
---|
143 | |
---|
144 | } |
---|