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

Last change on this file was 14176, checked in by Mark Evenson, 12 years ago

Refactor PATHNAME implementation details to tighten existing semantics.

None of this should change the behavior of CL:PATHNAME, but it
prepares for that in subsequent patches to address problems in merging
when the defaults points to a JAR-PATHNAME.

Fix COMPILE-FILE to work with source located in jar archive.

Moved Utilities.getFile() to instance method of Pathname which makes
more logical sense. Moved Utilities.getPathnameDirectory() to static
instance classes. These functions no longer merge their argument with
*DEFAULT-PATHNAME-DEFAULTS*, as this should be done explictly at a
higher level in the Lisp calling into Java abstraction.

RENAME-FILE no longer on namestrings, but instead use the result of
TRUENAME invocation, as namestrings will not always roundtrip
exactly back to PATHNAMES.

POPULATE-ZIP-FASL no longer forms its argumentes by merging paths,
instead using MAKE-PATHNAME with controlled defaults.

SYSTEM:NEXT-CLASSFILE-NAME and SYSTEM:COMPUTE-CLASSFILE-NAME changed
to NEXT-CLASSFILE and COMPUTE-CLASSFILE returning PATHNAME objects
rather than namestrings.

Compiler now dumps pathname in alternate form that preserves DEVICE
:UNSPECIFIC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.7 KB
Line 
1/*
2 * Utilities.java
3 *
4 * Copyright (C) 2003-2007 Peter Graves
5 * $Id: Utilities.java 14176 2012-10-11 11:33:19Z 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., 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 ZipInputStream getZipInputStream(ZipFile zipfile,
94                                                   String entryName) {
95        return Utilities.getZipInputStream(zipfile, entryName, false);
96    }
97
98  public static ZipInputStream getZipInputStream(ZipFile zipfile,
99                                                 String entryName,
100                                                 boolean errorOnFailure) {
101    ZipEntry zipEntry = zipfile.getEntry(entryName);
102    ZipInputStream stream = null;
103    try {
104      stream = new ZipInputStream(zipfile.getInputStream(zipEntry));
105    } catch (IOException e) {
106      if (errorOnFailure) {
107          Lisp.error(new FileError("Failed to open '" + entryName + "' in zipfile '"
108                                   + zipfile + "': " + e.getMessage()));
109      }
110      return null;
111    }
112    return stream;
113  }
114
115  public static InputStream getEntryAsInputStream(ZipInputStream zipInputStream,
116                                                  String entryName)
117    {
118        ZipEntry entry = getEntry(zipInputStream, entryName);
119        ByteArrayOutputStream bytes = readEntry(zipInputStream);
120        return new ByteArrayInputStream(bytes.toByteArray());
121
122    }
123
124    public static ByteArrayOutputStream readEntry(ZipInputStream stream) {
125        ByteArrayOutputStream result = new ByteArrayOutputStream();
126        int count;
127        byte buf[] = new byte[1024];
128        try {
129            while ((count = stream.read(buf, 0, buf.length)) != -1) {
130                result.write(buf, 0, count);
131            }
132        } catch (java.io.IOException e) {
133            Debug.trace("Failed to read entry from " 
134                        + stream
135                        + ": " + e);
136            return null;
137        }
138        return result;
139    }
140
141    public static ZipEntry getEntry(ZipInputStream zipInputStream, String entryName) {
142        return Utilities.getEntry(zipInputStream, entryName, false);
143    }
144
145  public static ZipEntry getEntry(ZipInputStream zipInputStream,
146                                  String entryName,
147                                  boolean errorOnFailure)
148  {
149    ZipEntry entry = null;
150    do {
151      try {
152        entry = zipInputStream.getNextEntry();
153      } catch (IOException e) {
154        if (errorOnFailure) {
155          Lisp.error(new FileError("Failed to seek for "
156            + "'" + entryName + "'"
157            + " in " + zipInputStream.toString()));
158        }
159        return null;
160      }
161    } while (entry != null && !entry.getName().equals(entryName));
162    if (entry != null) {
163      return entry;
164    }
165    if (errorOnFailure) {
166      Lisp.error(new FileError("Failed to find "
167        + "'" + entryName + "'"
168        + " in " + zipInputStream.toString()));
169    }
170    return null;
171
172  }
173   
174    public static final boolean checkZipFile(Pathname name) {
175        InputStream input = name.getInputStream();
176        try {
177            byte[] bytes = new byte[4];
178            int bytesRead = input.read(bytes);
179            return (bytesRead == 4
180                    && bytes[0] == 0x50
181                    && bytes[1] == 0x4b
182                    && bytes[2] == 0x03
183                    && bytes[3] == 0x04);
184        } catch (Throwable t) { // any error probably means 'no'
185            return false;
186        } finally {
187            if (input != null) {
188                try {
189                    input.close();
190                }
191                catch (IOException e) {} // ignore exceptions
192            }
193        }
194    }
195
196    static InputStream getInputStream(ZipFile jarFile, Pathname inner) {
197        String entryPath = inner.asEntryPath();
198        ZipEntry entry = jarFile.getEntry(entryPath);
199        if (entry == null) {
200            Debug.trace("Failed to find entry "
201                    + "'" + entryPath + "'"
202                    + " in " 
203                    + "'" + jarFile.getName() + "'");
204            return null;
205        }
206        InputStream result = null;
207        try {
208            result = jarFile.getInputStream(entry);
209        } catch (IOException e) {
210            Debug.trace("Failed to open InputStream for "
211              + "'" + entryPath + "'"
212              + " in "
213              + "'" + jarFile.getName() + "'");
214            return null;
215        }
216        return result;
217    }
218
219    static String escapeFormat(String s) {
220        return s.replace("~", "~~");
221    }
222}
Note: See TracBrowser for help on using the repository browser.