|
From: <kc...@us...> - 2003-06-13 06:52:40
|
Update of /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/packaging
In directory sc8-pr-cvs1:/tmp/cvs-serv9974/packaging
Modified Files:
AttachmentDataSource.java
Log Message:
added a flag to all constructors which pass in a file: to let user choose
whether the content of the file has to be loaded during construction or not.
Default the content will not be loaded, and data will be read from the file
only when the InputStream is consumed.
Index: AttachmentDataSource.java
===================================================================
RCS file: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/packaging/AttachmentDataSource.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** AttachmentDataSource.java 9 Apr 2003 07:48:17 -0000 1.8
--- AttachmentDataSource.java 13 Jun 2003 06:52:37 -0000 1.9
***************
*** 87,96 ****
* Content type of the attachment.
*/
! private final String contentType;
/**
* Content-Transfer-Encoding of the attachment.
*/
! private final String encoding;
/**
--- 87,96 ----
* Content type of the attachment.
*/
! private String contentType;
/**
* Content-Transfer-Encoding of the attachment.
*/
! private String encoding;
/**
***************
*** 102,110 ****
* Attachment data.
*/
! private final byte[] data;
! private final long offset;
! private final long length;
/**
--- 102,162 ----
* Attachment data.
*/
! private byte[] data;
! private long offset;
! private long length;
!
! /**
! * Constructs an <code>AttachmentDataSource</code> object from an array
! * of binary data.
! *
! * @param data Byte array containing data for the
! * <code>AttachmentDataSource</code>
! * @param contentType Content type of the data.
! */
! public AttachmentDataSource(byte[] data, String contentType) {
! this(data, contentType, null);
! }
!
! /**
! * Constructs an <code>AttachmentDataSource</code> object from an array
! * of binary data, and assign a name to the data source.
! *
! * @param data Byte array containing data for the
! * <code>AttachmentDataSource</code>
! * @param contentType Content type of the data.
! * @param name Name assigned to the
! * <code>AttachmentDataSource</code>
! */
! public AttachmentDataSource(byte[] data, String contentType, String name) {
! this(data, contentType, null, name);
! }
!
! /**
! * Constructs an <code>AttachmentDataSource</code> object from an array
! * of binary data, and assign a name to the data source.
! *
! * @param data Byte array containing data for the
! * <code>AttachmentDataSource</code>
! * @param contentType Content type of the data.
! * @param encoding Content-Transfer-Encoding of the data.
! * @param name Name assigned to the
! * <code>AttachmentDataSource</code>
! */
! public AttachmentDataSource(byte[] data, String contentType,
! String encoding, String name) {
! this.contentType = contentType;
! this.encoding = encoding;
! if (name == null) {
! this.name = AttachmentDataSource.class.getName();
! }
! else {
! this.name = name;
! }
! this.data = data;
! offset = 0;
! length = data.length;
! }
/**
***************
*** 117,121 ****
public AttachmentDataSource(String fileName, String contentType)
throws IOException {
! this(new FileInputStream(fileName), contentType, fileName);
}
--- 169,187 ----
public AttachmentDataSource(String fileName, String contentType)
throws IOException {
! this(fileName, contentType, false);
! }
!
! /**
! * Constructs an <code>AttachmentDataSource</code> object from a file.
! *
! * @param fileName Name of the file to be loaded.
! * @param contentType Content type of the file.
! * @param loadToMem Load all data to memory upon creation
! * @throws IOException
! */
! public AttachmentDataSource(String fileName, String contentType,
! boolean loadToMem)
! throws IOException {
! this(new File(fileName), contentType, loadToMem);
}
***************
*** 131,135 ****
public AttachmentDataSource(File file, String contentType)
throws IOException {
! this(new FileInputStream(file), contentType, file.getName());
}
--- 197,230 ----
public AttachmentDataSource(File file, String contentType)
throws IOException {
! this(file, contentType, false);
! }
!
! /**
! * Constructs an <code>AttachmentDataSource</code> object from a
! * <code>File</code> object.
! *
! * @param file <code>File</code> object containing information
! * on the file to be loaded.
! * @param contentType Content type of the file.
! * @param loadToMem Load all data to memory upon creation
! * @throws IOException
! */
! public AttachmentDataSource(File file, String contentType,
! boolean loadToMem)
! throws IOException {
!
! this.contentType = contentType;
! this.encoding = null;
!
! if (loadToMem) {
! this.name = file.getName();
! loadData(new FileInputStream(file));
! }
! else {
! this.name = file.getCanonicalPath();
! this.data = null;
! this.offset = 0;
! this.length = file.length();
! }
}
***************
*** 189,257 ****
this.name = name;
}
! final byte[] buffer = new byte[4096];
! final ByteArrayOutputStream out = new ByteArrayOutputStream();
! for (int c=in.read(buffer) ; c!=-1 ; c=in.read(buffer))
! out.write(buffer, 0, c);
! data = out.toByteArray();
! offset = 0;
! length = data.length;
! }
!
! /**
! * Constructs an <code>AttachmentDataSource</code> object from an array
! * of binary data.
! *
! * @param data Byte array containing data for the
! * <code>AttachmentDataSource</code>
! * @param contentType Content type of the data.
! */
! public AttachmentDataSource(byte[] data, String contentType) {
! this(data, contentType, null);
}
/**
! * Constructs an <code>AttachmentDataSource</code> object from an array
! * of binary data, and assign a name to the data source.
*
! * @param data Byte array containing data for the
! * <code>AttachmentDataSource</code>
* @param contentType Content type of the data.
- * @param name Name assigned to the
- * <code>AttachmentDataSource</code>
*/
! public AttachmentDataSource(byte[] data, String contentType, String name) {
! this(data, contentType, null, name);
}
/**
! * Constructs an <code>AttachmentDataSource</code> object from an array
! * of binary data, and assign a name to the data source.
*
! * @param data Byte array containing data for the
! * <code>AttachmentDataSource</code>
* @param contentType Content type of the data.
! * @param encoding Content-Transfer-Encoding of the data.
! * @param name Name assigned to the
! * <code>AttachmentDataSource</code>
*/
! public AttachmentDataSource(byte[] data, String contentType,
! String encoding, String name) {
! this.contentType = contentType;
! this.encoding = encoding;
! if (name == null) {
! this.name = AttachmentDataSource.class.getName();
! }
! else {
! this.name = name;
! }
! this.data = data;
! offset = 0;
! length = data.length;
}
/**
* Constructs an <code>AttachmentDataSource</code> object using the data
! * of <code>length</code> in a file starting from <code>offset</code>
! * with the specified Content-Type.
*
* @param fileName Name of the file to be loaded.
--- 284,327 ----
this.name = name;
}
! loadData(in);
}
/**
! * Constructs an <code>AttachmentDataSource</code> object using the data
! * of <code>length</code> in a file starting from <code>offset</code>
! * with the specified Content-Type.
*
! * @param fileName Name of the file to be loaded.
! * @param offset Offset from the start of the file.
! * @param length Length of data to be read.
* @param contentType Content type of the data.
*/
! public AttachmentDataSource(String fileName, long offset, long length,
! String contentType)
! throws IOException {
! this(fileName, offset, length, contentType, false);
}
/**
! * Constructs an <code>AttachmentDataSource</code> object using the data
! * of <code>length</code> in a file starting from <code>offset</code>
! * with the specified Content-Type.
*
! * @param fileName Name of the file to be loaded.
! * @param offset Offset from the start of the file.
! * @param length Length of data to be read.
* @param contentType Content type of the data.
! * @param loadToMem Load all data to memory upon creation
*/
! public AttachmentDataSource(String fileName, long offset, long length,
! String contentType, boolean loadToMem)
! throws IOException {
! this(fileName, offset, length, contentType, null, loadToMem);
}
/**
* Constructs an <code>AttachmentDataSource</code> object using the data
! * of <code>length</code> in a file starting from <code>offset</code>.
! * with the specified Content-Type and Content-Transfer-Encoding.
*
* @param fileName Name of the file to be loaded.
***************
*** 259,266 ****
* @param length Length of data to be read.
* @param contentType Content type of the data.
*/
public AttachmentDataSource(String fileName, long offset, long length,
! String contentType) {
! this(fileName, offset, length, contentType, null);
}
--- 329,338 ----
* @param length Length of data to be read.
* @param contentType Content type of the data.
+ * @param encoding Content-Transfer-Encoding of the data.
*/
public AttachmentDataSource(String fileName, long offset, long length,
! String contentType, String encoding)
! throws IOException {
! this(fileName, offset, length, contentType, encoding, false);
}
***************
*** 275,287 ****
* @param contentType Content type of the data.
* @param encoding Content-Transfer-Encoding of the data.
*/
public AttachmentDataSource(String fileName, long offset, long length,
! String contentType, String encoding) {
! this.contentType = contentType;
! this.encoding = encoding;
! this.name = fileName;
! this.data = null;
! this.offset = offset;
! this.length = length;
}
--- 347,387 ----
* @param contentType Content type of the data.
* @param encoding Content-Transfer-Encoding of the data.
+ * @param loadToMem Load all data to memory upon creation
*/
public AttachmentDataSource(String fileName, long offset, long length,
! String contentType, String encoding,
! boolean loadToMem)
! throws IOException {
! if (loadToMem) {
! final File file = new File(name);
! if (file.length() < (offset + length)) {
! throw new IOException("Premature end-of-file: file name=" +
! name + " | file length=" + file.length() + " | offset=" +
! offset + " | length to be read=" + length);
! }
! final FileInputStream fis = new FileInputStream(name);
! final InputStream is = new PartialInputStream(fis, offset, length);
! loadData(is);
! this.contentType = contentType;
! this.encoding = encoding;
! }
! else {
! this.contentType = contentType;
! this.encoding = encoding;
! this.name = fileName;
! this.data = null;
! this.offset = offset;
! this.length = length;
! }
! }
!
! private void loadData(InputStream is) throws IOException {
! final byte[] buffer = new byte[4096];
! final ByteArrayOutputStream out = new ByteArrayOutputStream();
! for (int c=is.read(buffer) ; c!=-1 ; c=is.read(buffer))
! out.write(buffer, 0, c);
! this.data = out.toByteArray();
! this.offset = 0;
! this.length = data.length;
}
***************
*** 386,391 ****
}
public long getLength() {
! return length;
}
}
--- 486,501 ----
}
+ /**
+ * Gets the length of data in this data source
+ *
+ * @return length of data
+ */
public long getLength() {
! if (data != null) {
! return data.length;
! }
! else {
! return length;
! }
}
}
|