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