introduced extended WritableResource interface

This commit is contained in:
Juergen Hoeller 2011-10-10 19:46:00 +00:00
parent f35dfd4107
commit 8bd1fc817a
3 changed files with 82 additions and 4 deletions

View File

@ -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 <code>java.io.File</code> 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.
*/

View File

@ -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 <code>false</code> is a definitive indication
* that the resource content cannot be read.
* @see #getInputStream()
*/
boolean isReadable();

View File

@ -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()}.
* <p>Will be <code>true</code> for typical resource descriptors;
* note that actual content writing may still fail when attempted.
* However, a value of <code>false</code> 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;
}