source: branches/1.1.x/src/org/armedbear/lisp/JarStream.java

Last change on this file was 13440, checked in by ehuelsmann, 13 years ago

Rename writeToString() to printObject() since that's what it's being used for.
Additionally, create princToString() for use in error messages, making the

required replacement where appropriate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/*
2 * JarStream.java
3 *
4 * Copyright (C) 2010 Mark Evenson
5 * $Id: JarStream.java 13440 2011-08-05 21:25:10Z 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
38import java.io.File;
39import java.io.InputStream;
40import java.io.Reader;
41import java.io.FileNotFoundException;
42import java.io.IOException;
43import java.io.InputStreamReader;
44import java.io.BufferedReader;
45
46/**
47 * Stream interface for an entry in a jar pathname.
48 *
49 * This only supports reading from the stream.
50 */
51public final class JarStream extends Stream
52{
53    private final Pathname pathname;
54    private final InputStream input;
55    private final Reader reader;
56    private final int bytesPerUnit;
57
58    public JarStream(Pathname pathname, String namestring,
59                          LispObject elementType, LispObject direction,
60                          LispObject ifExists, LispObject format)
61        throws IOException
62    {
63        super(Symbol.JAR_STREAM);
64        Debug.assertTrue(direction == Keyword.INPUT);
65        Debug.assertTrue(pathname.name != NIL);
66        isInputStream = true;
67
68        super.setExternalFormat(format);
69       
70        this.pathname = pathname;
71        this.elementType = elementType;
72
73        this.input = pathname.getInputStream();
74        if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR) {
75            isCharacterStream = true;
76            bytesPerUnit = 1;
77            InputStreamReader isr = new InputStreamReader(input);
78            this.reader = (Reader) new BufferedReader(isr);
79            initAsCharacterInputStream(this.reader);
80        } else {
81            isBinaryStream = true;
82            int width = Fixnum.getValue(elementType.cadr());
83            bytesPerUnit = width / 8;
84            this.reader = null;
85            initAsBinaryInputStream(this.input);
86        }
87    }
88
89    @Override
90    public LispObject typeOf()
91    {
92        return Symbol.JAR_STREAM;
93    }
94
95    @Override
96    public LispObject classOf()
97    {
98        return BuiltInClass.JAR_STREAM;
99    }
100
101    @Override
102    public LispObject typep(LispObject typeSpecifier)
103    {
104        if (typeSpecifier == Symbol.JAR_STREAM)
105            return T;
106        if (typeSpecifier == BuiltInClass.JAR_STREAM)
107            return T;
108        return super.typep(typeSpecifier);
109    }
110
111    @Override
112    public void setExternalFormat(LispObject format) {
113        super.setExternalFormat(format);
114    }
115
116    public Pathname getPathname()
117    {
118        return pathname;
119    }
120
121    @Override
122    public void _close()
123    {
124        try {
125            if (input != null) {
126                input.close();
127            }
128            if (reader != null) {
129                reader.close();
130            }
131            setOpen(false);
132        }
133        catch (IOException e) {
134            error(new StreamError(this, e));
135        }
136    }
137
138    @Override
139    public String printObject()
140    {
141        StringBuffer sb = new StringBuffer();
142        sb.append(Symbol.JAR_STREAM.princToString());
143        String namestring = pathname.getNamestring();
144        if (namestring != null) {
145            sb.append(" ");
146            sb.append(namestring);
147        }
148        return unreadableString(sb.toString());
149    }
150}
Note: See TracBrowser for help on using the repository browser.