01: package com.sun.istack;
02:
03: import javax.activation.DataSource;
04: import java.io.InputStream;
05: import java.io.ByteArrayInputStream;
06: import java.io.OutputStream;
07:
08: /**
09: * {@link DataSource} backed by a byte buffer.
10: *
11: * @author Kohsuke Kawaguchi
12: */
13: public final class ByteArrayDataSource implements DataSource {
14:
15: private final String contentType;
16: private final byte[] buf;
17: private final int len;
18:
19: public ByteArrayDataSource(byte[] buf, String contentType) {
20: this (buf, buf.length, contentType);
21: }
22:
23: public ByteArrayDataSource(byte[] buf, int length,
24: String contentType) {
25: this .buf = buf;
26: this .len = length;
27: this .contentType = contentType;
28: }
29:
30: public String getContentType() {
31: if (contentType == null)
32: return "application/octet-stream";
33: return contentType;
34: }
35:
36: public InputStream getInputStream() {
37: return new ByteArrayInputStream(buf, 0, len);
38: }
39:
40: public String getName() {
41: return null;
42: }
43:
44: public OutputStream getOutputStream() {
45: throw new UnsupportedOperationException();
46: }
47: }
|