Skip to content

Commit 81861ca

Browse files
committed
Resource "contentLength()" implementations work with OSGi bundle resources and JBoss VFS resources (SPR-9118)
1 parent 4831ca2 commit 81861ca

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public long contentLength() throws IOException {
152152
URL url = getURL();
153153
if (ResourceUtils.isFileURL(url)) {
154154
// Proceed with file system resolution...
155-
return super.contentLength();
155+
return getFile().length();
156156
}
157157
else {
158158
// Try a URL connection content-length header...

org.springframework.core/src/main/java/org/springframework/core/io/AbstractResource.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.URL;
2626

2727
import org.springframework.core.NestedIOException;
28+
import org.springframework.util.FileCopyUtils;
2829
import org.springframework.util.ResourceUtils;
2930

3031
/**
@@ -108,12 +109,13 @@ public File getFile() throws IOException {
108109
}
109110

110111
/**
111-
* This implementation checks the length of the underlying File,
112-
* if available.
113-
* @see #getFile()
112+
* This implementation reads the entire InputStream to calculate the
113+
* content length. Subclasses will almost always be able to provide
114+
* a more optimal version of this, e.g. checking a File length.
115+
* @see #getInputStream()
114116
*/
115117
public long contentLength() throws IOException {
116-
return getFile().length();
118+
return FileCopyUtils.copyToByteArray(getInputStream()).length;
117119
}
118120

119121
/**

org.springframework.core/src/main/java/org/springframework/core/io/FileSystemResource.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -139,6 +139,14 @@ public File getFile() {
139139
return this.file;
140140
}
141141

142+
/**
143+
* This implementation returns the underlying File's length.
144+
*/
145+
@Override
146+
public long contentLength() throws IOException {
147+
return this.file.length();
148+
}
149+
142150
/**
143151
* This implementation creates a FileSystemResource, applying the given path
144152
* relative to the path of the underlying file of this resource descriptor.

org.springframework.core/src/main/java/org/springframework/core/io/VfsResource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public File getFile() throws IOException {
8686
return VfsUtils.getFile(this.resource);
8787
}
8888

89+
@Override
90+
public long contentLength() throws IOException {
91+
return VfsUtils.getSize(this.resource);
92+
}
93+
8994
@Override
9095
public long lastModified() throws IOException {
9196
return VfsUtils.getLastModified(this.resource);

org.springframework.core/src/main/java/org/springframework/core/io/VfsUtils.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727

2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
30+
3031
import org.springframework.core.NestedIOException;
3132
import org.springframework.util.ReflectionUtils;
3233

@@ -58,14 +59,15 @@ private static enum VFS_VER { V2, V3 }
5859
private static Method VFS_METHOD_GET_ROOT_URI = null;
5960

6061
private static Method VIRTUAL_FILE_METHOD_EXISTS = null;
62+
private static Method VIRTUAL_FILE_METHOD_GET_INPUT_STREAM;
6163
private static Method VIRTUAL_FILE_METHOD_GET_SIZE;
6264
private static Method VIRTUAL_FILE_METHOD_GET_LAST_MODIFIED;
63-
private static Method VIRTUAL_FILE_METHOD_GET_CHILD;
64-
private static Method VIRTUAL_FILE_METHOD_GET_INPUT_STREAM;
6565
private static Method VIRTUAL_FILE_METHOD_TO_URL;
6666
private static Method VIRTUAL_FILE_METHOD_TO_URI;
6767
private static Method VIRTUAL_FILE_METHOD_GET_NAME;
6868
private static Method VIRTUAL_FILE_METHOD_GET_PATH_NAME;
69+
private static Method VIRTUAL_FILE_METHOD_GET_CHILD;
70+
6971
protected static Class<?> VIRTUAL_FILE_VISITOR_INTERFACE;
7072
protected static Method VIRTUAL_FILE_METHOD_VISIT;
7173

@@ -101,9 +103,10 @@ private static enum VFS_VER { V2, V3 }
101103

102104
if (logger.isDebugEnabled())
103105
logger.debug("JBoss VFS packages for JBoss AS 5 found");
104-
} catch (ClassNotFoundException ex1) {
106+
}
107+
catch (ClassNotFoundException ex2) {
105108
logger.error("JBoss VFS packages (for both JBoss AS 5 and 6) were not found - JBoss VFS support disabled");
106-
throw new IllegalStateException("Cannot detect JBoss VFS packages", ex1);
109+
throw new IllegalStateException("Cannot detect JBoss VFS packages", ex2);
107110
}
108111
}
109112

@@ -117,8 +120,8 @@ private static enum VFS_VER { V2, V3 }
117120
Class<?> virtualFile = loader.loadClass(pkg + "VirtualFile");
118121

119122
VIRTUAL_FILE_METHOD_EXISTS = ReflectionUtils.findMethod(virtualFile, "exists");
120-
VIRTUAL_FILE_METHOD_GET_SIZE = ReflectionUtils.findMethod(virtualFile, "getSize");
121123
VIRTUAL_FILE_METHOD_GET_INPUT_STREAM = ReflectionUtils.findMethod(virtualFile, "openStream");
124+
VIRTUAL_FILE_METHOD_GET_SIZE = ReflectionUtils.findMethod(virtualFile, "getSize");
122125
VIRTUAL_FILE_METHOD_GET_LAST_MODIFIED = ReflectionUtils.findMethod(virtualFile, "getLastModified");
123126
VIRTUAL_FILE_METHOD_TO_URI = ReflectionUtils.findMethod(virtualFile, "toURI");
124127
VIRTUAL_FILE_METHOD_TO_URL = ReflectionUtils.findMethod(virtualFile, "toURL");
@@ -183,6 +186,10 @@ static boolean isReadable(Object vfsResource) {
183186
}
184187
}
185188

189+
static long getSize(Object vfsResource) throws IOException {
190+
return (Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_SIZE, vfsResource);
191+
}
192+
186193
static long getLastModified(Object vfsResource) throws IOException {
187194
return (Long) invokeVfsMethod(VIRTUAL_FILE_METHOD_GET_LAST_MODIFIED, vfsResource);
188195
}

0 commit comments

Comments
 (0)