source: branches/0.22.x/abcl/src/org/armedbear/lisp/ByteArrayOutputStream.java

Last change on this file was 12513, checked in by ehuelsmann, 15 years ago

Remove 'private' keyword to eliminate the Java requirement

for the compiler to generate synthetic accessors: functions that
don't appear in the source but do appear in the class file.

Patch by: Douglas Miles <dmiles _at_ users.sf.net>

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1/*
2 * ByteArrayOutputStream.java
3 *
4 * Copyright (C) 2009 Alessio Stalla
5 * $Id: ByteArrayOutputStream.java 12513 2010-03-02 22:35:36Z 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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public 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    @Override
96    public String toString()
97    {
98        return unreadableString("BYTE-ARRAY-OUTPUT-STREAM");
99    }
100
101    // ### %make-byte-array-output-stream
102    // %make-byte-array-output-stream &optional element-type => byte-array-output-stream
103    private static final Primitive MAKE_BYTE_ARRAY_OUTPUT_STREAM =
104        new Primitive("%make-byte-array-output-stream", PACKAGE_SYS, false,
105                       "&optional element-type")
106    {
107
108        @Override
109        public LispObject execute() {
110            return new ByteArrayOutputStream();
111        }
112
113        @Override
114        public LispObject execute(LispObject arg)
115        {
116            return new ByteArrayOutputStream(arg);
117        }
118    };
119
120    // ### %get-output-stream-bytes
121    // %get-output-stream-bytes byte-array-output-stream => java-byte-array
122    private static final Primitive GET_OUTPUT_STREAM_STRING =
123        new Primitive("%get-output-stream-bytes", PACKAGE_SYS, false,
124                       "byte-array-output-stream")
125    {
126        @Override
127        public LispObject execute(LispObject arg)
128        {
129            if (arg instanceof ByteArrayOutputStream) {
130                return JavaObject.getInstance(((ByteArrayOutputStream)arg).getByteArray());
131            }
132            return error(new TypeError(this, Symbol.STREAM)); //TODO
133        }
134    };
135
136    private static final Primitive GET_OUTPUT_STREAM_ARRAY =
137        new Primitive("%get-output-stream-array", PACKAGE_SYS, false,
138                      "byte-array-output-stream")
139    {
140        @Override
141        public LispObject execute(LispObject arg)
142        {
143            if (arg instanceof ByteArrayOutputStream)
144                return new BasicVector_UnsignedByte8(((ByteArrayOutputStream)arg).getByteArray());
145
146            return error(new TypeError(this, Symbol.STREAM)); // TODO
147        }
148    };
149
150}
Note: See TracBrowser for help on using the repository browser.