diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java b/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java index f5c5f18034..a09934359b 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java @@ -152,7 +152,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource { URL url = getURL(); if (ResourceUtils.isFileURL(url)) { // Proceed with file system resolution... - return super.contentLength(); + return getFile().length(); } else { // Try a URL connection content-length header... diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/AbstractResource.java b/org.springframework.core/src/main/java/org/springframework/core/io/AbstractResource.java index 2f6c8e7d7d..e2360b4748 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/AbstractResource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/AbstractResource.java @@ -25,6 +25,7 @@ import java.net.URISyntaxException; import java.net.URL; import org.springframework.core.NestedIOException; +import org.springframework.util.FileCopyUtils; import org.springframework.util.ResourceUtils; /** @@ -108,12 +109,13 @@ public abstract class AbstractResource implements Resource { } /** - * This implementation checks the length of the underlying File, - * if available. - * @see #getFile() + * This implementation reads the entire InputStream to calculate the + * content length. Subclasses will almost always be able to provide + * a more optimal version of this, e.g. checking a File length. + * @see #getInputStream() */ public long contentLength() throws IOException { - return getFile().length(); + return FileCopyUtils.copyToByteArray(getInputStream()).length; } /** diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/FileSystemResource.java b/org.springframework.core/src/main/java/org/springframework/core/io/FileSystemResource.java index d96ba7c75b..3d0805c345 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/FileSystemResource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/FileSystemResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -139,6 +139,14 @@ public class FileSystemResource extends AbstractResource implements WritableReso return this.file; } + /** + * This implementation returns the underlying File's length. + */ + @Override + public long contentLength() throws IOException { + return this.file.length(); + } + /** * This implementation creates a FileSystemResource, applying the given path * relative to the path of the underlying file of this resource descriptor. diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/VfsResource.java b/org.springframework.core/src/main/java/org/springframework/core/io/VfsResource.java index c161858ad6..88925f6b78 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/VfsResource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/VfsResource.java @@ -86,6 +86,11 @@ public class VfsResource extends AbstractResource { return VfsUtils.getFile(this.resource); } + @Override + public long contentLength() throws IOException { + return VfsUtils.getSize(this.resource); + } + @Override public long lastModified() throws IOException { return VfsUtils.getLastModified(this.resource); diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/VfsUtils.java b/org.springframework.core/src/main/java/org/springframework/core/io/VfsUtils.java index a0e268ce70..71c2ceb097 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/VfsUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/VfsUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import java.net.URL; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.core.NestedIOException; import org.springframework.util.ReflectionUtils; @@ -58,14 +59,15 @@ public abstract class VfsUtils { private static Method VFS_METHOD_GET_ROOT_URI = null; private static Method VIRTUAL_FILE_METHOD_EXISTS = null; + private static Method VIRTUAL_FILE_METHOD_GET_INPUT_STREAM; private static Method VIRTUAL_FILE_METHOD_GET_SIZE; private static Method VIRTUAL_FILE_METHOD_GET_LAST_MODIFIED; - private static Method VIRTUAL_FILE_METHOD_GET_CHILD; - private static Method VIRTUAL_FILE_METHOD_GET_INPUT_STREAM; private static Method VIRTUAL_FILE_METHOD_TO_URL; private static Method VIRTUAL_FILE_METHOD_TO_URI; private static Method VIRTUAL_FILE_METHOD_GET_NAME; private static Method VIRTUAL_FILE_METHOD_GET_PATH_NAME; + private static Method VIRTUAL_FILE_METHOD_GET_CHILD; + protected static Class VIRTUAL_FILE_VISITOR_INTERFACE; protected static Method VIRTUAL_FILE_METHOD_VISIT; @@ -101,9 +103,10 @@ public abstract class VfsUtils { if (logger.isDebugEnabled()) logger.debug("JBoss VFS packages for JBoss AS 5 found"); - } catch (ClassNotFoundException ex1) { + } + catch (ClassNotFoundException ex2) { logger.error("JBoss VFS packages (for both JBoss AS 5 and 6) were not found - JBoss VFS support disabled"); - throw new IllegalStateException("Cannot detect JBoss VFS packages", ex1); + throw new IllegalStateException("Cannot detect JBoss VFS packages", ex2); } } @@ -117,8 +120,8 @@ public abstract class VfsUtils { Class virtualFile = loader.loadClass(pkg + "VirtualFile"); VIRTUAL_FILE_METHOD_EXISTS = ReflectionUtils.findMethod(virtualFile, "exists"); - VIRTUAL_FILE_METHOD_GET_SIZE = ReflectionUtils.findMethod(virtualFile, "getSize"); VIRTUAL_FILE_METHOD_GET_INPUT_STREAM = ReflectionUtils.findMethod(virtualFile, "openStream"); + VIRTUAL_FILE_METHOD_GET_SIZE = ReflectionUtils.findMethod(virtualFile, "getSize"); VIRTUAL_FILE_METHOD_GET_LAST_MODIFIED = ReflectionUtils.findMethod(virtualFile, "getLastModified"); VIRTUAL_FILE_METHOD_TO_URI = ReflectionUtils.findMethod(virtualFile, "toURI"); VIRTUAL_FILE_METHOD_TO_URL = ReflectionUtils.findMethod(virtualFile, "toURL"); @@ -183,6 +186,10 @@ public abstract class VfsUtils { } } + static long getSize(Object vfsResource) throws IOException { + return (Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_SIZE, vfsResource); + } + static long getLastModified(Object vfsResource) throws IOException { return (Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_LAST_MODIFIED, vfsResource); }