
com.sirika.httpclienthelpers.springframework.InputStreamSourceBody Maven / Gradle / Ivy
/**
* Copyright 2009 Sami Dalouche
*
* 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.sirika.httpclienthelpers.springframework;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.http.entity.mime.MIME;
import org.apache.http.entity.mime.content.AbstractContentBody;
import org.apache.james.mime4j.message.BinaryBody;
import org.springframework.core.io.InputStreamSource;
public class InputStreamSourceBody extends AbstractContentBody implements BinaryBody {
private final InputStreamSource inputStreamSource;
private final String filename;
public InputStreamSourceBody(final InputStreamSource inputStreamSource, final String mimeType, final String filename) {
super(mimeType);
if (inputStreamSource == null) {
throw new IllegalArgumentException("Input stream source may not be null");
}
this.inputStreamSource = inputStreamSource;
this.filename = filename;
}
public InputStreamSourceBody(final InputStreamSource inputStreamSource, final String filename) {
this(inputStreamSource, "application/octet-stream", filename);
}
public InputStream getInputStream() throws IOException {
return this.inputStreamSource.getInputStream();
}
public void writeTo(final OutputStream out, int mode) throws IOException {
if (out == null) {
throw new IllegalArgumentException("Output stream may not be null");
}
InputStream in = null;
try {
in = this.inputStreamSource.getInputStream();
IOUtils.copy(in, out);
out.flush();
} finally {
IOUtils.closeQuietly(in);
}
}
public String getTransferEncoding() {
return MIME.ENC_BINARY;
}
public String getCharset() {
return null;
}
public long getContentLength() {
return -1;
}
public String getFilename() {
return this.filename;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy