package org.springframework.core.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Convenience base class for Resource implementations,
 * pre-implementing typical behavior.
 *
 * <p>The "exists" method will check whether an InputStream can be opened;
 * "isOpen" will always return false; "getFile" throws an exception;
 * and "toString" will return the description.
 *
 * @author Juergen Hoeller
 * @since 28.12.2003
 */
public abstract class AbstractResource implements Resource {

	/**
	 * This implementations checks whether an InputStream can be opened.
	 */
	public boolean exists() {
		try {
			InputStream is = getInputStream();
			is.close();
			return true;
		}
		catch (IOException ex) {
			return false;
		}
	}

	/**
	 * This implementations always returns false.
	 */
	public boolean isOpen() {
		return false;
	}

	/**
	 * This implementations throws a FileNotFoundException, assuming
	 * that the resource cannot be resolved to an absolute file path.
	 */
	public File getFile() throws IOException {
		throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
	}

	/**
	 * This implementations returns the description of this resource.
	 * @see #getDescription
	 */
	public String toString() {
		return getDescription();
	}

}

@@ -286,33 +286,40 @@
  check whether a
-n
+ File or
  InputStream can
@@ -306,32 +306,35 @@
 e or InputStream
+%0A *
  can be opened;%0A
@@ -332,19 +332,16 @@
  opened;
-%0A *
  %22isOpen
@@ -384,16 +384,19 @@
 %22 throws
+%0A *
  an exce
@@ -401,19 +401,16 @@
 ception;
-%0A *
  and %22to
@@ -591,17 +591,16 @@
 entation
-s
  checks 
@@ -612,77 +612,318 @@
 er a
-n InputStream can be opened.%0A%09 */%0A%09public boolean exists() %7B%0A%09%09try %7B%0A
+ File can be opened,%0A%09 * falling back to whether an InputStream can be opened.%0A%09 * This will cover both directories and content resources.%0A%09 */%0A%09public boolean exists() %7B%0A%09%09// try file existence%0A%09%09try %7B%0A%09%09%09return getFile().exists();%0A%09%09%7D%0A%09%09catch (IOException ex) %7B%0A%09%09%09// fall back to stream existence%0A%09%09%09try %7B%0A%09
 %09%09%09I
@@ -959,16 +959,17 @@
 m();%0A%09%09%09
+%09
 is.close
@@ -972,16 +972,17 @@
 lose();%0A
+%09
 %09%09%09retur
@@ -989,22 +989,24 @@
 n true;%0A
+%09
 %09%09%7D%0A%09%09
+%09
 catch (I
@@ -1018,23 +1018,25 @@
 ption ex
+2
 ) %7B%0A%09%09%09
+%09
 return f
@@ -1041,16 +1041,21 @@
  false;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

check for File existence too in "exists" to cover directories