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

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/*
2 * Utilities.java
3 *
4 * Copyright (C) 2003-2007 Peter Graves
5 * $Id: Utilities.java 12254 2009-11-06 20:07:54Z 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 java.io.ByteArrayInputStream;
37import java.io.ByteArrayOutputStream;
38import java.io.File;
39import java.io.IOException;
40import java.io.InputStream;
41import java.util.zip.ZipEntry;
42import java.util.zip.ZipFile;
43import java.util.zip.ZipInputStream;
44
45public final class Utilities extends Lisp
46{
47    public static final boolean isPlatformUnix;
48    public static final boolean isPlatformWindows;
49
50    static {
51        String osName = System.getProperty("os.name");
52        isPlatformUnix = osName.startsWith("Linux") ||
53            osName.startsWith("Mac OS X") || osName.startsWith("Darwin") ||
54            osName.startsWith("Solaris") ||
55            osName.startsWith("SunOS") || osName.startsWith("AIX") ||
56            osName.startsWith("FreeBSD") || osName.startsWith("OpenBSD") ||
57            osName.startsWith("NetBSD");
58        isPlatformWindows = osName.startsWith("Windows");
59    }
60
61    public static boolean isFilenameAbsolute(String filename)
62    {
63        final int length = filename.length();
64        if (length > 0) {
65            char c0 = filename.charAt(0);
66            if (c0 == '\\' || c0 == '/')
67                return true;
68            if (length > 2) {
69                if (isPlatformWindows) {
70                    // Check for drive letter.
71                    char c1 = filename.charAt(1);
72                    if (c1 == ':') {
73                        if (c0 >= 'a' && c0 <= 'z')
74                            return true;
75                        if (c0 >= 'A' && c0 <= 'Z')
76                            return true;
77                    }
78                } else {
79                    // Unix.
80                    if (filename.equals("~") || filename.startsWith("~/"))
81                        return true;
82                }
83            }
84        }
85        return false;
86    }
87
88    public static File getFile(Pathname pathname)
89    {
90        return getFile(pathname,
91                       coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue()));
92    }
93
94    public static File getFile(Pathname pathname, Pathname defaultPathname)
95
96    {
97        Pathname merged =
98            Pathname.mergePathnames(pathname, defaultPathname, NIL);
99        String namestring = merged.getNamestring();
100        if (namestring != null)
101            return new File(namestring);
102        error(new FileError("Pathname has no namestring: " + merged.writeToString(),
103                             merged));
104        // Not reached.
105        return null;
106    }
107
108    public static Pathname getDirectoryPathname(File file)
109
110    {
111        try {
112            String namestring = file.getCanonicalPath();
113            if (namestring != null && namestring.length() > 0) {
114                if (namestring.charAt(namestring.length() - 1) != File.separatorChar)
115                    namestring = namestring.concat(File.separator);
116            }
117            return new Pathname(namestring);
118        }
119        catch (IOException e) {
120            error(new LispError(e.getMessage()));
121            // Not reached.
122            return null;
123        }
124    }
125   
126    public static byte[] getZippedZipEntryAsByteArray(ZipFile zipfile,
127                                                      String entryName,
128                                                      String subEntryName) 
129
130  {
131      ZipEntry entry = zipfile.getEntry(entryName);
132     
133      ZipInputStream stream = null;
134      try {
135          stream = new ZipInputStream(zipfile.getInputStream(entry));
136      } 
137      catch (IOException e) {
138          Lisp.error(new FileError("Failed to open '" + entryName + "' in zipfile '"
139                                   + zipfile + "': " + e.getMessage()));
140      }
141      //  XXX Cache the zipEntries somehow
142      do {
143          try { 
144              entry = stream.getNextEntry();
145          } catch (IOException e){
146              Lisp.error(new FileError("Failed to seek for '" + subEntryName
147                                       + "' in '" 
148                                       + zipfile.getName() + ":" + entryName + ".:"
149                                       + e.getMessage()));
150          }
151      } while (!entry.getName().equals(subEntryName));
152     
153      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
154        int count;
155        byte buf[] = new byte[1024];
156        try {
157            while ((count = stream.read(buf, 0, buf.length)) != -1) {
158                buffer.write(buf, 0, count);
159            }
160        } catch (java.io.IOException e) {
161          Lisp.error(new FileError("Failed to read compressed '"
162                                   + subEntryName
163                                   + "' in '" 
164                                   + zipfile.getName() + ":" + entryName + ":"
165                                   + e.getMessage()));
166        }
167        return buffer.toByteArray();
168    }
169   
170    public static InputStream getZippedZipEntryAsInputStream(ZipFile zipfile,
171                                                             String entryName,
172                                                             String subEntryName) 
173
174  {
175        return 
176            new ByteArrayInputStream(Utilities
177                                     .getZippedZipEntryAsByteArray(zipfile, entryName, 
178                                                                   subEntryName));
179  }
180}
181
Note: See TracBrowser for help on using the repository browser.