From 8bd1fc817a5ce16f0b41dba1d30ab249027c5084 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 10 Oct 2011 19:46:00 +0000 Subject: [PATCH] introduced extended WritableResource interface --- .../core/io/FileSystemResource.java | 28 +++++++++- .../org/springframework/core/io/Resource.java | 6 ++- .../core/io/WritableResource.java | 52 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 org.springframework.core/src/main/java/org/springframework/core/io/WritableResource.java 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 4ebfbc2d445..d96ba7c75b1 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-2008 the original author or authors. + * Copyright 2002-2011 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. @@ -18,8 +18,10 @@ package org.springframework.core.io; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.URI; import java.net.URL; @@ -29,12 +31,13 @@ import org.springframework.util.StringUtils; /** * {@link Resource} implementation for java.io.File handles. * Obviously supports resolution as File, and also as URL. + * Implements the extended {@link WritableResource} interface. * * @author Juergen Hoeller * @since 28.12.2003 * @see java.io.File */ -public class FileSystemResource extends AbstractResource { +public class FileSystemResource extends AbstractResource implements WritableResource { private final File file; @@ -166,6 +169,27 @@ public class FileSystemResource extends AbstractResource { } + // implementation of WritableResource + + /** + * This implementation checks whether the underlying file is marked as writable + * (and corresponds to an actual file with content, not to a directory). + * @see java.io.File#canWrite() + * @see java.io.File#isDirectory() + */ + public boolean isWritable() { + return (this.file.canWrite() && !this.file.isDirectory()); + } + + /** + * This implementation opens a FileOutputStream for the underlying file. + * @see java.io.FileOutputStream + */ + public OutputStream getOutputStream() throws IOException { + return new FileOutputStream(this.file); + } + + /** * This implementation compares the underlying File references. */ diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/Resource.java b/org.springframework.core/src/main/java/org/springframework/core/io/Resource.java index e57a9776449..aafb1a9dab4 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/Resource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/Resource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -35,12 +35,13 @@ import java.net.URL; * @see #getURL() * @see #getURI() * @see #getFile() + * @see WritableResource + * @see ContextResource * @see FileSystemResource * @see ClassPathResource * @see UrlResource * @see ByteArrayResource * @see InputStreamResource - * @see org.springframework.web.context.support.ServletContextResource */ public interface Resource extends InputStreamSource { @@ -59,6 +60,7 @@ public interface Resource extends InputStreamSource { * note that actual content reading may still fail when attempted. * However, a value of false is a definitive indication * that the resource content cannot be read. + * @see #getInputStream() */ boolean isReadable(); diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/WritableResource.java b/org.springframework.core/src/main/java/org/springframework/core/io/WritableResource.java new file mode 100644 index 00000000000..c7ec3722c02 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/io/WritableResource.java @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.io; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Extended interface for a resource that supports writing to it. + * Provides an {@link #getOutputStream() OutputStream accessor}. + * + * @author Juergen Hoeller + * @since 3.1 + * @see java.io.OutputStream + */ +public interface WritableResource extends Resource { + + /** + * Return whether the contents of this resource can be modified, + * e.g. via {@link #getOutputStream()} or {@link #getFile()}. + *

Will be true for typical resource descriptors; + * note that actual content writing may still fail when attempted. + * However, a value of false is a definitive indication + * that the resource content cannot be modified. + * @see #getOutputStream() + * @see #isReadable() + */ + boolean isWritable(); + + /** + * Return an {@link OutputStream} for the underlying resource, + * allowing to (over-)write its content. + * @throws IOException if the stream could not be opened + * @see #getInputStream() + */ + OutputStream getOutputStream() throws IOException; + +}