All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wl4g.infra.common.resource.AbstractStreamResource Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 ~ 2025 the original author or authors. James Wong 
 *
 * 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 com.wl4g.infra.common.resource;

/**
 * Retention of upstream license agreement statement:
* Thank you very much spring framework, We fully comply with and support the open license * agreement of spring. The purpose of migration is to solve the problem * that these elegant API programs can still be easily used without running * in the spring environment. *
* Copyright 2002-2017 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. */ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import com.wl4g.infra.common.lang.Assert2; /** * Convenience base class for * {@link org.AbstractStreamResource.io.AbstractResource} implementations, * pre-implementing typical behavior. * *

* The "exists" method will check whether a File or InputStream can be opened; * "isOpen" will always return false; "getURL" and "getFile" throw an exception; * and "toString" will return the description. * * @author Juergen Hoeller * @since 28.12.2003 */ abstract class AbstractStreamResource implements StreamResource { /** * This implementation checks whether a File can be opened, falling back to * whether an InputStream can be opened. This will cover both directories * and content resources. */ @Override public boolean exists() { // Try file existence: can we find the file in the file system? try { return getFile().exists(); } catch (IOException ex) { // Fall back to stream existence: can we open the stream? try { InputStream is = getInputStream(); is.close(); return true; } catch (Throwable isEx) { return false; } } } /** * This implementation always returns {@code true}. */ @Override public boolean isReadable() { return true; } /** * This implementation always returns {@code false}. */ @Override public boolean isOpen() { return false; } /** * This implementation throws a FileNotFoundException, assuming that the * resource cannot be resolved to a URL. */ @Override public URL getURL() throws IOException { throw new FileNotFoundException(getDescription() + " cannot be resolved to URL"); } /** * This implementation builds a URI based on the URL returned by * {@link #getURL()}. */ @Override public URI getURI() throws IOException { URL url = getURL(); try { return ResourceUtils2.toURI(url); } catch (URISyntaxException ex) { throw new IllegalArgumentException("Invalid URI [" + url + "]", ex); } } /** * This implementation throws a FileNotFoundException, assuming that the * resource cannot be resolved to an absolute file path. */ @Override public File getFile() throws IOException { throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path"); } /** * 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() */ @Override public long contentLength() throws IOException { InputStream is = getInputStream(); Assert2.state(is != null, "Resource InputStream must not be null"); try { long size = 0; byte[] buf = new byte[255]; int read; while ((read = is.read(buf)) != -1) { size += read; } return size; } finally { try { is.close(); } catch (IOException ex) { } } } /** * This implementation checks the timestamp of the underlying File, if * available. * * @see #getFileForLastModifiedCheck() */ @Override public long lastModified() throws IOException { long lastModified = getFileForLastModifiedCheck().lastModified(); if (lastModified == 0L) { throw new FileNotFoundException( getDescription() + " cannot be resolved in the file system for resolving its last-modified timestamp"); } return lastModified; } /** * Determine the File to use for timestamp checking. *

* The default implementation delegates to {@link #getFile()}. * * @return the File to use for timestamp checking (never {@code null}) * @throws FileNotFoundException * if the resource cannot be resolved as an absolute file path, * i.e. is not available in a file system * @throws IOException * in case of general resolution/reading failures */ protected File getFileForLastModifiedCheck() throws IOException { return getFile(); } /** * This implementation throws a FileNotFoundException, assuming that * relative resources cannot be created for this resource. */ @Override public StreamResource createRelative(String relativePath) throws IOException { throw new FileNotFoundException("Cannot create a relative resource for " + getDescription()); } /** * This implementation always returns {@code null}, assuming that this * resource type does not have a filename. */ @Override public String getFilename() { return null; } /** * This implementation returns the description of this resource. * * @see #getDescription() */ @Override public String toString() { return getDescription(); } /** * This implementation compares description strings. * * @see #getDescription() */ @Override public boolean equals(Object obj) { return (obj == this || (obj instanceof StreamResource && ((StreamResource) obj).getDescription().equals(getDescription()))); } /** * This implementation returns the description's hash code. * * @see #getDescription() */ @Override public int hashCode() { return getDescription().hashCode(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy