source: branches/1.0.x/abcl/src/org/armedbear/lisp/Utilities.java

Last change on this file was 13440, checked in by ehuelsmann, 14 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: 8.9 KB
Line 
1/*
2 * Utilities.java
3 *
4 * Copyright (C) 2003-2007 Peter Graves
5 * $Id: Utilities.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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.ByteArrayInputStream;
39import java.io.ByteArrayOutputStream;
40import java.io.File;
41import java.io.IOException;
42import java.io.InputStream;
43import java.net.URI;
44import java.net.URISyntaxException;
45import java.util.jar.JarFile;
46import java.util.zip.ZipEntry;
47import java.util.zip.ZipFile;
48import java.util.zip.ZipInputStream;
49
50public final class Utilities
51{
52    public static final boolean isPlatformUnix;
53    public static final boolean isPlatformWindows;
54
55    static {
56        String osName = System.getProperty("os.name");
57        isPlatformUnix = osName.startsWith("Linux") ||
58            osName.startsWith("Mac OS X") || osName.startsWith("Darwin") ||
59            osName.startsWith("Solaris") ||
60            osName.startsWith("SunOS") || osName.startsWith("AIX") ||
61            osName.startsWith("FreeBSD") || osName.startsWith("OpenBSD") ||
62            osName.startsWith("NetBSD");
63        isPlatformWindows = osName.startsWith("Windows");
64    }
65
66    public static boolean isFilenameAbsolute(String filename)
67    {
68        final int length = filename.length();
69        if (length > 0) {
70            char c0 = filename.charAt(0);
71            if (c0 == '\\' || c0 == '/')
72                return true;
73            if (length > 2) {
74                if (isPlatformWindows) {
75                    // Check for drive letter.
76                    char c1 = filename.charAt(1);
77                    if (c1 == ':') {
78                        if (c0 >= 'a' && c0 <= 'z')
79                            return true;
80                        if (c0 >= 'A' && c0 <= 'Z')
81                            return true;
82                    }
83                } else {
84                    // Unix.
85                    if (filename.equals("~") || filename.startsWith("~/"))
86                        return true;
87                }
88            }
89        }
90        return false;
91    }
92
93    public static File getFile(Pathname pathname)
94    {
95        return getFile(pathname,
96                       coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue()));
97    }
98
99    public static File getFile(Pathname pathname, Pathname defaultPathname)
100
101    {
102        Pathname merged =
103            Pathname.mergePathnames(pathname, defaultPathname, NIL);
104        String namestring = merged.getNamestring();
105        if (namestring != null)
106            return new File(namestring);
107        error(new FileError("Pathname has no namestring: " + merged.princToString(),
108                             merged));
109        // Not reached.
110        return null;
111    }
112
113    public static Pathname getDirectoryPathname(File file)
114
115    {
116        try {
117            String namestring = file.getCanonicalPath();
118            if (namestring != null && namestring.length() > 0) {
119                if (namestring.charAt(namestring.length() - 1) != File.separatorChar)
120                    namestring = namestring.concat(File.separator);
121            }
122            return new Pathname(namestring);
123        }
124        catch (IOException e) {
125            error(new LispError(e.getMessage()));
126            // Not reached.
127            return null;
128        }
129    }
130
131    public static ZipInputStream getZipInputStream(ZipFile zipfile,
132                                                   String entryName) {
133        return Utilities.getZipInputStream(zipfile, entryName, false);
134    }
135
136  public static ZipInputStream getZipInputStream(ZipFile zipfile,
137                                                 String entryName,
138                                                 boolean errorOnFailure) {
139    ZipEntry zipEntry = zipfile.getEntry(entryName);
140    ZipInputStream stream = null;
141    try {
142      stream = new ZipInputStream(zipfile.getInputStream(zipEntry));
143    } catch (IOException e) {
144      if (errorOnFailure) {
145          Lisp.error(new FileError("Failed to open '" + entryName + "' in zipfile '"
146                                   + zipfile + "': " + e.getMessage()));
147      }
148      return null;
149    }
150    return stream;
151  }
152
153  public static InputStream getEntryAsInputStream(ZipInputStream zipInputStream,
154                                                  String entryName)
155    {
156        ZipEntry entry = getEntry(zipInputStream, entryName);
157        ByteArrayOutputStream bytes = readEntry(zipInputStream);
158        return new ByteArrayInputStream(bytes.toByteArray());
159
160    }
161
162    public static ByteArrayOutputStream readEntry(ZipInputStream stream) {
163        ByteArrayOutputStream result = new ByteArrayOutputStream();
164        int count;
165        byte buf[] = new byte[1024];
166        try {
167            while ((count = stream.read(buf, 0, buf.length)) != -1) {
168                result.write(buf, 0, count);
169            }
170        } catch (java.io.IOException e) {
171            Debug.trace("Failed to read entry from " 
172                        + stream
173                        + ": " + e);
174            return null;
175        }
176        return result;
177    }
178
179    public static ZipEntry getEntry(ZipInputStream zipInputStream, String entryName) {
180        return Utilities.getEntry(zipInputStream, entryName, false);
181    }
182
183  public static ZipEntry getEntry(ZipInputStream zipInputStream,
184                                  String entryName,
185                                  boolean errorOnFailure)
186  {
187    ZipEntry entry = null;
188    do {
189      try {
190        entry = zipInputStream.getNextEntry();
191      } catch (IOException e) {
192        if (errorOnFailure) {
193          Lisp.error(new FileError("Failed to seek for "
194            + "'" + entryName + "'"
195            + " in " + zipInputStream.toString()));
196        }
197        return null;
198      }
199    } while (entry != null && !entry.getName().equals(entryName));
200    if (entry != null) {
201      return entry;
202    }
203    if (errorOnFailure) {
204      Lisp.error(new FileError("Failed to find "
205        + "'" + entryName + "'"
206        + " in " + zipInputStream.toString()));
207    }
208    return null;
209
210  }
211   
212    public static final boolean checkZipFile(Pathname name) {
213        InputStream input = name.getInputStream();
214        try {
215            byte[] bytes = new byte[4];
216            int bytesRead = input.read(bytes);
217            return (bytesRead == 4
218                    && bytes[0] == 0x50
219                    && bytes[1] == 0x4b
220                    && bytes[2] == 0x03
221                    && bytes[3] == 0x04);
222        } catch (Throwable t) { // any error probably means 'no'
223            return false;
224        } finally {
225            if (input != null) {
226                try {
227                    input.close();
228                }
229                catch (IOException e) {} // ignore exceptions
230            }
231        }
232    }
233
234    static InputStream getInputStream(ZipFile jarFile, Pathname inner) {
235        String entryPath = inner.asEntryPath();
236        ZipEntry entry = jarFile.getEntry(entryPath);
237        if (entry == null) {
238            Debug.trace("Failed to find entry "
239                    + "'" + entryPath + "'"
240                    + " in " 
241                    + "'" + jarFile.getName() + "'");
242            return null;
243        }
244        InputStream result = null;
245        try {
246            result = jarFile.getInputStream(entry);
247        } catch (IOException e) {
248            Debug.trace("Failed to open InputStream for "
249              + "'" + entryPath + "'"
250              + " in "
251              + "'" + jarFile.getName() + "'");
252            return null;
253        }
254        return result;
255    }
256
257    static String escapeFormat(String s) {
258        return s.replace("~", "~~");
259    }
260}
Note: See TracBrowser for help on using the repository browser.