Thursday, May 12, 2011

Simple implementation of DataSource in Java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;
public class ByteArrayDataSource implements DataSource {
private String contentType;
private byte data[];
private int length;
private int offset;
public ByteArrayDataSource(String contentType, byte data[], int offset, int length) {
this.contentType = contentType;
this.data = data;
this.offset = offset;
this.length = length;
}
public ByteArrayDataSource(String contentType, byte data[], int offset, int length,
ByteArrayDataSource byteArrayDataSource) {
this(contentType, data, offset, length);
}
public String getContentType() {
return contentType;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data, offset, length);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
}

No comments:

Post a Comment