You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by wc...@apache.org on 2001/01/13 17:14:10 UTC
cvs commit: xml-soap/java/src/org/apache/soap/util/mime ByteArrayDataSource.java MimeUtils.java
wcloeten 01/01/13 08:14:10
Modified: java/src/org/apache/soap/util/mime ByteArrayDataSource.java
MimeUtils.java
Log:
added ByteArrayDataSource.getText() utility function, adjusted source to coding standard
Revision Changes Path
1.2 +72 -40 xml-soap/java/src/org/apache/soap/util/mime/ByteArrayDataSource.java
Index: ByteArrayDataSource.java
===================================================================
RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/mime/ByteArrayDataSource.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ByteArrayDataSource.java 2000/11/28 22:31:04 1.1
+++ ByteArrayDataSource.java 2001/01/13 16:14:10 1.2
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 2000 The Apache Software Foundation. All rights
+ * Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -18,7 +18,7 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
+ * if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
@@ -26,7 +26,7 @@
*
* 4. The names "SOAP" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
- * software without prior written permission. For written
+ * software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
@@ -71,34 +71,38 @@
* @author Wouter Cloetens (wcloeten@raleigh.ibm.com)
*/
-public class ByteArrayDataSource implements DataSource
-{
+public class ByteArrayDataSource implements DataSource {
private byte[] data; // data
private String type; // content-type
/**
- * Create a datasource from a File.
+ * Create a datasource from a File. If the Content-Type parameter is null,
+ * the type will be derived from the filename extension.
+ *
+ * @param f File object
+ * @param type Content-Type
*/
- public ByteArrayDataSource(File f, String type) throws IOException
- {
+ public ByteArrayDataSource(File f, String type) throws IOException {
this(new FileInputStream(f), type);
- if(this.type == null)
+ if (this.type == null)
this.type = FileTypeMap.getDefaultFileTypeMap().getContentType(f);
}
/**
* Create a datasource from an input stream.
+ *
+ * @param is InputStream
+ * @param type Content-Type
*/
- public ByteArrayDataSource(InputStream is, String type) throws IOException
- {
+ public ByteArrayDataSource(InputStream is, String type)
+ throws IOException {
this.type = type;
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte buf[] = new byte[4096];
int len;
- while (true)
- {
+ while (true){
len = is.read(buf);
if (len < 0)
break;
@@ -109,6 +113,9 @@
/**
* Create a datasource from a byte array.
+ *
+ * @param data byte array
+ * @param type Content-Type
*/
public ByteArrayDataSource(byte[] data, String type)
{
@@ -118,88 +125,113 @@
/**
* Create a datasource from a String. This method defaults to
- * encoding the String as iso-8859-1. For a different encoding,
+ * a String encoding of iso-8859-1. For a different encoding,
* specify a Mime "charset" in the Content-Type parameter.
+ *
+ * @param data byte array
+ * @param type Content-Type
*/
public ByteArrayDataSource(String data, String type)
{
this.type = type;
- try
- {
+ try {
this.data = data.getBytes(MimeUtils.getEncoding(type,
- Constants.HEADERVAL_DEFAULT_CHARSET));
+ Constants.HEADERVAL_DEFAULT_CHARSET));
+ } catch (UnsupportedEncodingException uex) {
}
- catch (UnsupportedEncodingException uex)
- {
- }
}
/**
* Return an InputStream to read the content.
+ *
+ * @return an InputStream with the content
*/
- public InputStream getInputStream() throws IOException
- {
+ public InputStream getInputStream() throws IOException {
if (data == null)
throw new IOException("No data.");
return new ByteArrayInputStream(data);
}
/**
- * This DataSource cannot return an OutputStream.
+ * This DataSource cannot return an OutputStream, so this method is not
+ * implemented.
*/
- public OutputStream getOutputStream() throws IOException
- {
+ public OutputStream getOutputStream() throws IOException {
throw new IOException("getOutputStream() not supported.");
}
/**
* Get the content type.
+ *
+ * @return Content-Type string
*/
- public String getContentType()
- {
+ public String getContentType() {
return type;
}
/**
* Set the content type.
+ *
+ * @param Content-Type string
*/
- public void setContentType(String type)
- {
+ public void setContentType(String type) {
this.type = type;
}
/**
- * getName() is not supported.
+ * getName() is not implemented.
*/
- public String getName()
- {
+ public String getName() {
return "";
}
/**
* Write the content to an OutputStream.
+ *
+ * @param os OutputStream to write the entire content to
*/
- public void writeTo(OutputStream os) throws IOException
- {
+ public void writeTo(OutputStream os) throws IOException {
os.write(data);
}
/**
* Return the content as a byte array.
+ *
+ * @return byte array with the content
*/
- public byte[] toByteArray()
- {
+ public byte[] toByteArray() {
return data;
}
/**
- * Return the size of the byte array.
+ * Return the number of bytes in the content.
+ *
+ * @return size of the byte array, or -1 if not set.
*/
- public int getSize()
- {
+ public int getSize() {
if (data == null)
return -1;
else
return data.length;
+ }
+
+ /**
+ * Return the content as a String. The Content-Type "charset" parameter
+ * will be used to determine the encoding, and if that's not available or
+ * invalid, "iso-8859-1".
+ *
+ * @return a String with the content
+ */
+ public String getText() {
+ try {
+ return new String(data, MimeUtils.getEncoding(type,
+ Constants.HEADERVAL_DEFAULT_CHARSET));
+ } catch (UnsupportedEncodingException uex) {
+ try {
+ return new String(data, Constants.HEADERVAL_DEFAULT_CHARSET);
+ } catch (UnsupportedEncodingException uex1) {
+ return null;
+ }
+ }
}
}
1.2 +14 -26 xml-soap/java/src/org/apache/soap/util/mime/MimeUtils.java
Index: MimeUtils.java
===================================================================
RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/mime/MimeUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MimeUtils.java 2000/11/28 22:31:04 1.1
+++ MimeUtils.java 2001/01/13 16:14:10 1.2
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 2000 The Apache Software Foundation. All rights
+ * Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -18,7 +18,7 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
+ * if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
@@ -26,7 +26,7 @@
*
* 4. The names "SOAP" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
- * software without prior written permission. For written
+ * software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
@@ -70,8 +70,7 @@
* @author Wouter Cloetens (wcloeten@raleigh.ibm.com)
*/
-public class MimeUtils
-{
+public class MimeUtils {
/**
* Get a unique value.
*
@@ -81,16 +80,12 @@
* time (in milliseconds), and this system's hostname generated by
* <code>InternetAddress.getLocalAddress()</code>.
*/
- public static String getUniqueValue()
- {
+ public static String getUniqueValue() {
String user = System.getProperty("user.name");
String host = null;
- try
- {
+ try {
host = InetAddress.getLocalHost().getHostName();
- }
- catch (UnknownHostException uhe)
- {
+ } catch (UnknownHostException uhe) {
host = "localhost";
}
@@ -105,16 +100,12 @@
/**
* Extract a Java encoding from a Content-Type's MIME character set.
*/
- public static String getEncoding(String type, String defaultEncoding)
- {
+ public static String getEncoding(String type, String defaultEncoding) {
String encoding = null;
- try
- {
+ try {
if (type != null && !type.equals(""))
encoding = new ContentType(type).getParameter("charset");
- }
- catch(ParseException pe)
- {
+ } catch(ParseException pe) {
}
if (encoding == null)
encoding = defaultEncoding;
@@ -128,15 +119,12 @@
* URLDecode string.
*/
private static final String hexmap = "0123456789ABCDEF";
- public static String decode(String s)
- {
+ public static String decode(String s) {
StringBuffer ret = new StringBuffer();
char c;
- for (int i = 0; i < s.length(); i++)
- {
+ for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
- switch(c)
- {
+ switch(c) {
case '+':
ret.append(' ');
break;