source: trunk/abcl/src/org/armedbear/lisp/ByteArrayOutputStream.java

Last change on this file was 15569, checked in by Mark Evenson, 2 years ago

Untabify en masse

Results of running style.org source blocks on tree

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