
infra.beans.propertyeditors.InputStreamEditor Maven / Gradle / Ivy
/*
* Copyright 2017 - 2024 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [https://www.gnu.org/licenses/]
*/
package infra.beans.propertyeditors;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import infra.core.io.Resource;
import infra.core.io.ResourceEditor;
import infra.core.io.ResourceLoader;
import infra.lang.Assert;
import infra.lang.Nullable;
/**
* One-way PropertyEditor which can convert from a text String to a
* {@code java.io.InputStream}, interpreting the given String as a
* Framework resource location (e.g. a URL String).
*
* Supports Framework-style URL notation: any fully qualified standard URL
* ("file:", "http:", etc.) and Framework's special "classpath:" pseudo-URL.
*
*
Note that such streams usually do not get closed by Framework itself!
*
* @author Juergen Hoeller
* @see java.io.InputStream
* @see ResourceEditor
* @see ResourceLoader
* @see URLEditor
* @see FileEditor
* @since 4.0
*/
public class InputStreamEditor extends PropertyEditorSupport {
private final ResourceEditor resourceEditor;
/**
* Create a new InputStreamEditor, using the default ResourceEditor underneath.
*/
public InputStreamEditor() {
this.resourceEditor = new ResourceEditor();
}
/**
* Create a new InputStreamEditor, using the given ResourceEditor underneath.
*
* @param resourceEditor the ResourceEditor to use
*/
public InputStreamEditor(ResourceEditor resourceEditor) {
Assert.notNull(resourceEditor, "ResourceEditor is required");
this.resourceEditor = resourceEditor;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
this.resourceEditor.setAsText(text);
Resource resource = (Resource) this.resourceEditor.getValue();
try {
setValue(resource != null ? resource.getInputStream() : null);
}
catch (IOException ex) {
throw new IllegalArgumentException("Failed to retrieve InputStream for " + resource, ex);
}
}
/**
* This implementation returns {@code null} to indicate that
* there is no appropriate text representation.
*/
@Override
@Nullable
public String getAsText() {
return null;
}
}