KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > attachments > OctetStreamDataSource


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8 package org.jboss.axis.attachments;
9
10 import javax.activation.DataSource JavaDoc;
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16
17 public class OctetStreamDataSource implements DataSource JavaDoc
18 {
19    public static final String JavaDoc CONTENT_TYPE = "application/octetstream";
20
21    private final String JavaDoc name;
22    private byte[] data;
23    private ByteArrayOutputStream JavaDoc os;
24
25    public OctetStreamDataSource(String JavaDoc name, OctetStream data)
26    {
27       this.name = name;
28       this.data = data == null ? null : data.getBytes();
29       os = new ByteArrayOutputStream JavaDoc();
30    } // ctor
31

32    public String JavaDoc getName()
33    {
34       return name;
35    } // getName
36

37    public String JavaDoc getContentType()
38    {
39       return CONTENT_TYPE;
40    } // getContentType
41

42    public InputStream JavaDoc getInputStream() throws IOException JavaDoc
43    {
44       if (os.size() != 0)
45       {
46          data = os.toByteArray();
47       }
48       return new ByteArrayInputStream JavaDoc(data == null ? new byte[0] : data);
49    } // getInputStream
50

51    public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
52    {
53       if (os.size() != 0)
54       {
55          data = os.toByteArray();
56       }
57       return new ByteArrayOutputStream JavaDoc();
58    } // getOutputStream
59
} // class OctetStreamDataSource
60
Popular Tags