summaryrefslogtreecommitdiff
path: root/gnu/java/net/protocol/file/Connection.java
diff options
context:
space:
mode:
authorMichael Koch <konqueror@gmx.de>2003-10-18 10:04:09 +0000
committerMichael Koch <konqueror@gmx.de>2003-10-18 10:04:09 +0000
commit9e981aa163e1aa744eb28725cb4c82d1d4ee491a (patch)
tree5b54822ab8cc3372c42e34a7c6e11322b843ff45 /gnu/java/net/protocol/file/Connection.java
parenta840e13389b068fc9c0cc0b4e3bb186031db19fe (diff)
downloadclasspath-9e981aa163e1aa744eb28725cb4c82d1d4ee491a.tar.gz
2003-10-18 Michael Koch <konqueror@gmx.de>
* gnu/java/net/protocol/file/FileURLConnection.java, gnu/java/net/protocol/http/HttpURLConnection.java, gnu/java/net/protocol/jar/JarURLConnection.java: Removed. * gnu/java/net/protocol/file/Connection.java, gnu/java/net/protocol/http/Connection.java, gnu/java/net/protocol/jar/Connection.java: New files. * gnu/java/net/protocol/jar/Handler.java, gnu/java/net/protocol/http/Handler.java, gnu/java/net/protocol/file/Handler.java, gnu/java/lang/SystemClassLoader.java: Use new files.
Diffstat (limited to 'gnu/java/net/protocol/file/Connection.java')
-rw-r--r--gnu/java/net/protocol/file/Connection.java279
1 files changed, 279 insertions, 0 deletions
diff --git a/gnu/java/net/protocol/file/Connection.java b/gnu/java/net/protocol/file/Connection.java
new file mode 100644
index 000000000..e6f866239
--- /dev/null
+++ b/gnu/java/net/protocol/file/Connection.java
@@ -0,0 +1,279 @@
+/*
+ FileURLConnection.java -- URLConnection class for "file" protocol
+ Copyright (C) 1998, 2003 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ GNU Classpath is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU Classpath is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Classpath; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library. Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module. An independent module is a module which is not derived from
+ or based on this library. If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so. If you do not wish to do so, delete this
+ exception statement from your version.
+*/
+package gnu.java.net.protocol.file;
+
+import java.net.URL;
+import java.net.URLConnection;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.AbstractSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.NoSuchElementException;
+
+/**
+ * This subclass of java.net.URLConnection models a URLConnection via
+ * the "file" protocol.
+ *
+ * @author Aaron M. Renn (arenn@urbanophile.com)
+ * @author Nic Ferrier (nferrier@tapsellferrier.co.uk)
+ */
+public class Connection extends URLConnection
+{
+ /**
+ * This is a File object for this connection
+ */
+ private File file;
+
+ /**
+ * InputStream if we are reading from the file
+ */
+ private FileInputStream in_stream;
+
+ /**
+ * OutputStream if we are writing to the file
+ */
+ private FileOutputStream out_stream;
+
+ /**
+ * Calls superclass constructor to initialize.
+ */
+ protected Connection (URL url)
+ {
+ super (url);
+
+ /* Set up some variables */
+ doOutput = false;
+ }
+
+ /**
+ * "Connects" to the file by opening it.
+ */
+ public void connect() throws IOException
+ {
+ if (connected)
+ return;
+
+ file = new File (getURL().getFile());
+
+ if (!file.exists())
+ throw new FileNotFoundException (file.getPath());
+
+ connected = true;
+ }
+
+ /**
+ * Opens the file for reading and returns a stream for it.
+ *
+ * @return An InputStream for this connection.
+ *
+ * @exception IOException If an error occurs
+ */
+ public InputStream getInputStream()
+ throws IOException
+ {
+ if (!connected)
+ connect();
+
+ in_stream = new FileInputStream (file);
+ return in_stream;
+ }
+
+ /**
+ * Opens the file for writing and returns a stream for it.
+ *
+ * @return An OutputStream for this connection.
+ *
+ * @exception IOException If an error occurs.
+ */
+ public OutputStream getOutputStream()
+ throws IOException
+ {
+ if (!connected)
+ connect();
+
+ out_stream = new FileOutputStream (file);
+ return out_stream;
+ }
+
+ /** Get the last modified time of the resource.
+ *
+ * @return the time since epoch that the resource was modified.
+ */
+ public long getLastModified()
+ {
+ try
+ {
+ if (!connected)
+ connect();
+
+ return file.lastModified();
+ }
+ catch (IOException e)
+ {
+ return -1;
+ }
+ }
+
+ /** Get the length of content.
+ *
+ * @return the length of the content.
+ */
+ public int getContentLength()
+ {
+ try
+ {
+ if (!connected)
+ connect();
+
+ return (int) file.length();
+ }
+ catch (IOException e)
+ {
+ return -1;
+ }
+ }
+
+ // These are GNU only implementation methods.
+
+ /** Does the resource pointed to actually exist?
+ */
+ public final boolean exists()
+ {
+ if (file == null)
+ return false;
+
+ return file.exists();
+ }
+
+ /**
+ * Is the resource pointed to a directory?
+ */
+ public final boolean isDirectory()
+ {
+ return file.isDirectory();
+ }
+
+ /** Get a listing of the directory, if it is a directory.
+ *
+ * @return a set which can supply an iteration of the
+ * contents of the directory.
+ *
+ * @throws IllegalStateException if this is not pointing
+ * to a directory.
+ */
+ public Set getListing()
+ {
+ if (!file.isDirectory())
+ throw new IllegalStateException ("this is not a directory");
+
+ final File[] directoryList = file.listFiles();
+ return new AbstractSet()
+ {
+ File[] dirList = directoryList;
+
+ public int size()
+ {
+ return dirList.length;
+ }
+
+ public Iterator iterator()
+ {
+ return new Iterator()
+ {
+ int index = 0;
+
+ public boolean hasNext()
+ {
+ return index < dirList.length;
+ }
+
+ public Object next()
+ {
+ try
+ {
+ String value = dirList [index++].getName();
+ return value;
+ }
+ catch (ArrayIndexOutOfBoundsException e)
+ {
+ throw new NoSuchElementException ("no more content");
+ }
+ }
+
+ public void remove()
+ {
+ try
+ {
+ File[] newDirList = new File [dirList.length - 1];
+ int realIndex = index - 1;
+
+ if (realIndex < 1)
+ {
+ System.arraycopy (dirList, 1, newDirList, 0,
+ dirList.length - 1);
+ index--;
+ }
+ else
+ {
+ System.arraycopy (dirList, 0, newDirList, 0, realIndex);
+
+ if (index < dirList.length - 1)
+ System.arraycopy (dirList, index,
+ newDirList, realIndex,
+ dirList.length - realIndex);
+ }
+ dirList = newDirList;
+ }
+ catch (ArrayIndexOutOfBoundsException e)
+ {
+ throw new NoSuchElementException("no more content");
+ }
+ }
+ };
+ }
+ };
+ }
+
+} // class FileURLConnection