KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.attachments;
17
18 import javax.activation.DataSource JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 public class OctetStreamDataSource implements DataSource JavaDoc {
26     public static final String JavaDoc CONTENT_TYPE = "application/octet-stream";
27
28     private final String JavaDoc name;
29     private byte[] data;
30     private ByteArrayOutputStream JavaDoc os;
31
32     public OctetStreamDataSource(String JavaDoc name, OctetStream data) {
33         this.name = name;
34         this.data = data == null ? null : data.getBytes();
35         os = new ByteArrayOutputStream JavaDoc();
36     } // ctor
37

38     public String JavaDoc getName() {
39         return name;
40     } // getName
41

42     public String JavaDoc getContentType() {
43         return CONTENT_TYPE;
44     } // getContentType
45

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

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