/** * @see java.net.URLConnection#connect() */ @Override public void connect() throws IOException { if (!connected) { findJarFile(); // ensure the file can be found findJarEntry(); // ensure the entry, if any, can be found connected = true; } }
/** * Open a URLConnection on the given URL. */ @Override protected URLConnection openConnection(URL u) throws IOException { return new FtpURLConnection(u); }
/** * Returns the Jar file referred by this {@code URLConnection}. * * @throws IOException * thrown if an IO error occurs while connecting to the * resource. */ @Override public JarFile getJarFile() throws IOException { connect(); return jarFile; }
/** * Returns the length of the file in bytes. * * @return the length of the file * * @see #getContentType() */ @Override public int getContentLength() { try { if (!connected) { connect(); } } catch (IOException e) { // default is -1 } return length; }
/** * Creates a input stream for writing to this URL Connection. * * @return The input stream to write to * @throws IOException * Cannot read from URL or error creating InputStream * * @see #getContent() * @see #getOutputStream() * @see java.io.InputStream * @see java.io.IOException * */ @Override public InputStream getInputStream() throws IOException { if (!connected) { connect(); } return inputStream; }
/** * Returns a new path by resolving {@code path} relative to {@code base}. */ private static String relativePath(String base, String path) { if (path.startsWith("/")) { return UrlUtils.canonicalizePath(path, true); } else if (base != null) { String combined = base.substring(0, base.lastIndexOf('/') + 1) + path; return UrlUtils.canonicalizePath(combined, true); } else { return path; } }
private String validateScheme(String uri, int end) throws URISyntaxException { if (end == 0) { throw new URISyntaxException(uri, "Scheme expected", 0); } for (int i = 0; i < end; i++) { if (!UrlUtils.isValidSchemeChar(i, uri.charAt(i))) { throw new URISyntaxException(uri, "Illegal character in scheme", 0); } } return uri.substring(0, end); }
/** * Returns a connection to the a file pointed by this <code>URL</code> in * the file system * * @return A connection to the resource pointed by this url. * @param url * URL The URL to which the connection is pointing to * */ @Override public URLConnection openConnection(URL url) throws IOException { return openConnection(url, null); }
/** * Returns a connection to the jar file pointed by this <code>URL</code> * in the file system * * @return java.net.URLConnection A connection to the resource pointed by * this url. * @param u * java.net.URL The URL to which the connection is pointing to * * @throws IOException * thrown if an IO error occurs when this method tries to * establish connection. */ @Override protected URLConnection openConnection(URL u) throws IOException { return new JarURLConnectionImpl(u); }
/** * Returns the content type of the resource. Just takes a guess based on the * name. */ @Override public String getContentType() { String result = guessContentTypeFromName(url.getFile()); if (result == null) { return "content/unknown"; } return result; }
/** * @param url * the URL of the JAR * @throws MalformedURLException * if the URL is malformed * @throws IOException * if there is a problem opening the connection. */ public JarURLConnectionImpl(URL url) throws MalformedURLException, IOException { super(url); jarFileURL = getJarFileURL(); jarFileURLConnection = jarFileURL.openConnection(); }
/** * Look up the JarEntry of the entry referenced by this {@code * URLConnection}. */ private void findJarEntry() throws IOException { if (getEntryName() == null) { return; } jarEntry = jarFile.getJarEntry(getEntryName()); if (jarEntry == null) { throw new FileNotFoundException(getEntryName()); } }
private boolean readMultiLine() throws IOException { String line = readLine(); if (line.length() < 4) { return true; } if (line.substring(0, 3).equals(replyCode) && (line.charAt(3) == (char) 32)) { return false; } return true; }
@Override public void close() throws IOException { super.close(); if (!getUseCaches()) { closed = true; jarFile.close(); } } }
/** * Returns the JarEntry of the entry referenced by this {@code * URLConnection}. * * @return the JarEntry referenced * * @throws IOException * if an IO error occurs while getting the entry */ @Override public JarEntry getJarEntry() throws IOException { connect(); return jarEntry; }
/** * Returns the input stream of the object referred to by this * <code>URLConnection</code> * * File Sample : "/ZIP211/+/harmony/tools/javac/resources/javac.properties" * Invalid File Sample: * "/ZIP/+/harmony/tools/javac/resources/javac.properties" * "ZIP211/+/harmony/tools/javac/resources/javac.properties" * * @return input stream of the object * * @throws IOException * if an IO error occurs */ @Override public InputStream getInputStream() throws IOException { if (!connected) { connect(); } return is; }
/** * Creates a output stream for writing to this URL Connection. * * @return The output stream to write to * @throws IOException * when the OutputStream could not be created * * @see #getContent() * @see #getInputStream() * @see java.io.IOException * */ @Override public OutputStream getOutputStream() throws IOException { if (!connected) { connect(); } return dataSocket.getOutputStream(); }
/** * Returns the normalized path. */ private String normalize(String path, boolean discardRelativePrefix) { path = UrlUtils.canonicalizePath(path, discardRelativePrefix); /* * If the path contains a colon before the first colon, prepend * "./" to differentiate the path from a scheme prefix. */ int colon = path.indexOf(':'); if (colon != -1) { int slash = path.indexOf('/'); if (slash == -1 || colon < slash) { path = "./" + path; } } return path; }
/** * Returns the scheme prefix like "http" from the URL spec, or null if the * spec doesn't start with a scheme. Scheme prefixes match this pattern: * {@code alpha ( alpha | digit | '+' | '-' | '.' )* ':'} */ public static String getSchemePrefix(String spec) { int colon = spec.indexOf(':'); if (colon < 1) { return null; } for (int i = 0; i < colon; i++) { char c = spec.charAt(i); if (!isValidSchemeChar(i, c)) { return null; } } return spec.substring(0, colon).toLowerCase(Locale.US); }