Debian Codegraph @

demo/src/main/java/PlainDCH.java

raw
1 /*
2 * Copyright (c) 1997, 2019 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0, which is available at
6 * http://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 import java.io.*;
12 import jakarta.activation.*;
13
14
15 public class PlainDCH implements DataContentHandler {
16 /**
17 * return the ActivationDataFlavors for this <code>DataContentHandler</code>
18 * @return The ActivationDataFlavors.
19 */
20 public ActivationDataFlavor[] getTransferDataFlavors() {
21 ActivationDataFlavor flavors[] = new ActivationDataFlavor[2];
22
23
24 try {
25 flavors[0] = new ActivationDataFlavor(
26 Class.forName("java.lang.String"), "text/plain", "text string");
27 } catch(Exception e)
28 { System.out.println(e); }
29
30 flavors[1] = new ActivationDataFlavor("text/plain", "Plain Text");
31 return flavors;
32 }
33 /**
34 * return the Transfer Data of type ActivationDataFlavor from InputStream
35 * @param df The ActivationDataFlavor.
36 * @param ins The InputStream corresponding to the data.
37 * @return The constructed Object.
38 */
39 public Object getTransferData(ActivationDataFlavor df, DataSource ds) {
40
41 // this is sort of hacky, but will work for the
42 // sake of testing...
43 if(df.getMimeType().equals("text/plain")) {
44 if(df.getRepresentationClass().getName().equals(
45 "java.lang.String")) {
46 // spit out String
47 StringBuffer buf = new StringBuffer();
48 char data[] = new char[1024];
49 // InputStream is = null;
50 InputStreamReader isr = null;
51 int bytes_read = 0;
52 int total_bytes = 0;
53
54 try {
55 isr = new InputStreamReader(ds.getInputStream());
56
57 // while(is.read(data) > 0)
58 // buf.append(data);
59
60 while(true){
61 bytes_read = isr.read(data);
62 if(bytes_read > 0)
63 buf.append(data, total_bytes, bytes_read);
64 else
65 break;
66 total_bytes += bytes_read;
67 }
68 } catch(Exception e) {}
69
70 return buf.toString();
71
72 }
73 else if(df.getRepresentationClass().getName().equals(
74 "java.io.InputStream")){
75 // spit out InputStream
76 try {
77 return ds.getInputStream();
78 } catch (Exception e) {}
79 }
80
81 }
82
83 return null;
84 }
85
86 /**
87 *
88 */
89 public Object getContent(DataSource ds) { // throws Exception;
90 StringBuffer buf = new StringBuffer();
91 char data[] = new char[1024];
92 // InputStream is = null;
93 InputStreamReader isr = null;
94 int bytes_read = 0;
95 int total_bytes = 0;
96
97 try {
98 isr = new InputStreamReader(ds.getInputStream());
99
100 // while(is.read(data) > 0)
101 // buf.append(data);
102
103 while(true){
104 bytes_read = isr.read(data);
105 if(bytes_read > 0)
106 buf.append(data, total_bytes, bytes_read);
107 else
108 break;
109 total_bytes += bytes_read;
110 }
111 } catch(Exception e) {}
112
113 return buf.toString();
114 }
115 /**
116 * construct an object from a byte stream
117 * (similar semantically to previous method, we are deciding
118 * which one to support)
119 */
120 public void writeTo(Object obj, String mimeTye, OutputStream os)
121 throws IOException {
122 // throws Exception;
123 }
124
125 }