Skip to content

Commit 8bd1fc8

Browse files
committed
introduced extended WritableResource interface
1 parent f35dfd4 commit 8bd1fc8

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -18,8 +18,10 @@
1818

1919
import java.io.File;
2020
import java.io.FileInputStream;
21+
import java.io.FileOutputStream;
2122
import java.io.IOException;
2223
import java.io.InputStream;
24+
import java.io.OutputStream;
2325
import java.net.URI;
2426
import java.net.URL;
2527

@@ -29,12 +31,13 @@
2931
/**
3032
* {@link Resource} implementation for <code>java.io.File</code> handles.
3133
* Obviously supports resolution as File, and also as URL.
34+
* Implements the extended {@link WritableResource} interface.
3235
*
3336
* @author Juergen Hoeller
3437
* @since 28.12.2003
3538
* @see java.io.File
3639
*/
37-
public class FileSystemResource extends AbstractResource {
40+
public class FileSystemResource extends AbstractResource implements WritableResource {
3841

3942
private final File file;
4043

@@ -166,6 +169,27 @@ public String getDescription() {
166169
}
167170

168171

172+
// implementation of WritableResource
173+
174+
/**
175+
* This implementation checks whether the underlying file is marked as writable
176+
* (and corresponds to an actual file with content, not to a directory).
177+
* @see java.io.File#canWrite()
178+
* @see java.io.File#isDirectory()
179+
*/
180+
public boolean isWritable() {
181+
return (this.file.canWrite() && !this.file.isDirectory());
182+
}
183+
184+
/**
185+
* This implementation opens a FileOutputStream for the underlying file.
186+
* @see java.io.FileOutputStream
187+
*/
188+
public OutputStream getOutputStream() throws IOException {
189+
return new FileOutputStream(this.file);
190+
}
191+
192+
169193
/**
170194
* This implementation compares the underlying File references.
171195
*/

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

Lines changed: 4 additions & 2 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-2011 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.
@@ -35,12 +35,13 @@
3535
* @see #getURL()
3636
* @see #getURI()
3737
* @see #getFile()
38+
* @see WritableResource
39+
* @see ContextResource
3840
* @see FileSystemResource
3941
* @see ClassPathResource
4042
* @see UrlResource
4143
* @see ByteArrayResource
4244
* @see InputStreamResource
43-
* @see org.springframework.web.context.support.ServletContextResource
4445
*/
4546
public interface Resource extends InputStreamSource {
4647

@@ -59,6 +60,7 @@ public interface Resource extends InputStreamSource {
5960
* note that actual content reading may still fail when attempted.
6061
* However, a value of <code>false</code> is a definitive indication
6162
* that the resource content cannot be read.
63+
* @see #getInputStream()
6264
*/
6365
boolean isReadable();
6466

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.io;
18+
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
22+
/**
23+
* Extended interface for a resource that supports writing to it.
24+
* Provides an {@link #getOutputStream() OutputStream accessor}.
25+
*
26+
* @author Juergen Hoeller
27+
* @since 3.1
28+
* @see java.io.OutputStream
29+
*/
30+
public interface WritableResource extends Resource {
31+
32+
/**
33+
* Return whether the contents of this resource can be modified,
34+
* e.g. via {@link #getOutputStream()} or {@link #getFile()}.
35+
* <p>Will be <code>true</code> for typical resource descriptors;
36+
* note that actual content writing may still fail when attempted.
37+
* However, a value of <code>false</code> is a definitive indication
38+
* that the resource content cannot be modified.
39+
* @see #getOutputStream()
40+
* @see #isReadable()
41+
*/
42+
boolean isWritable();
43+
44+
/**
45+
* Return an {@link OutputStream} for the underlying resource,
46+
* allowing to (over-)write its content.
47+
* @throws IOException if the stream could not be opened
48+
* @see #getInputStream()
49+
*/
50+
OutputStream getOutputStream() throws IOException;
51+
52+
}

0 commit comments

Comments
 (0)