MessagePartpublic class MessagePart extends Object Instances of the MessagePart class can be added to a
MultipartMessage . Each MessagePart consists
of the content element, MIME type and content-id. The Content can be of
any type. Additionally, it's possible to specify the content location and
the encoding scheme. |
Fields Summary |
---|
static int | MAX_PART_SIZE_BYTESMaximum size for message part. | static final int | BUFFER_SIZEBuffer size 2048. | byte[] | contentContent byte array. | String | contentIDMIME Content ID. | String | contentLocationContent location. | String | encodingContent encoding. | String | mimeTypeMIME type. | static final char | US_ASCII_LOWEST_VALID_CHARLowest valid ASCII character. | static final char | US_ASCII_VALID_BIT_MASKMask for ASCII character checks. |
Constructors Summary |
---|
public MessagePart(byte[] contents, int offset, int length, String mimeType, String contentId, String contentLocation, String enc)Constructs a MessagePart object from a subset of the byte
array. This constructor is only useful if the data size is small
(roughly less than 10K). For larger content the InputStream
based constructor should be used.
construct(contents, offset, length, mimeType, contentId,
contentLocation, enc);
| public MessagePart(byte[] contents, String mimeType, String contentId, String contentLocation, String enc)Construct a MessagePart object from a byte array. This
constructor is only useful if the data size is small (roughly 10K).
For larger content the InputStream based constructor
should be used.
construct(contents, 0, (contents == null ? 0 : contents.length),
mimeType, contentId, contentLocation, enc);
| public MessagePart(InputStream is, String mimeType, String contentId, String contentLocation, String enc)Constructs a MessagePart object from an
InputStream . The contents of the MessagePart
are loaded from the InputStream during the constructor
call until the end of the stream is reached.
byte[] bytes = {};
if (is != null) {
ByteArrayOutputStream accumulator = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int readBytes = 0;
while ((readBytes = is.read(buffer)) != -1) {
accumulator.write(buffer, 0, readBytes);
}
bytes = accumulator.toByteArray();
}
construct(bytes, 0, bytes.length, mimeType, contentId,
contentLocation, enc);
|
Methods Summary |
---|
static void | checkContentID(java.lang.String contentId)Verifies the content identifier.
if (contentId == null) {
throw new IllegalArgumentException("contentId must be specified");
}
if (contentId.length() > 100) { // MMS Conformance limit
throw new IllegalArgumentException(
"contentId exceeds 100 char limit");
}
if (containsNonUSASCII(contentId)) {
throw new IllegalArgumentException(
"contentId must not contain non-US-ASCII characters");
}
| static void | checkContentLocation(java.lang.String contentLoc)Verifies the content location.
if (contentLoc != null) {
if (containsNonUSASCII(contentLoc)) {
throw new IllegalArgumentException(
"contentLocation must not contain non-US-ASCII characters");
}
if (contentLoc.length() > 100) { // MMS Conformance limit
throw new IllegalArgumentException(
"contentLocation exceeds 100 char limit");
}
}
| static void | checkEncodingScheme(java.lang.String encoding)Verifies the content encoding.
// IMPL_NOTE: check for a valid encoding scheme
| void | construct(byte[] contents, int offset, int length, java.lang.String mimeType, java.lang.String contentId, java.lang.String contentLocation, java.lang.String enc)Constructs a message. // 30K
if (length > MAX_PART_SIZE_BYTES) {
throw new SizeExceededException(
"InputStream data exceeds " +
"MessagePart size limit");
}
if (mimeType == null) {
throw new IllegalArgumentException("mimeType must be specified");
}
checkContentID(contentId);
checkContentLocation(contentLocation);
if (length < 0) {
throw new IllegalArgumentException("length must be >= 0");
}
if (contents != null && offset + length > contents.length) {
throw new IllegalArgumentException(
"offset + length exceeds contents length");
}
if (offset < 0) {
throw new IllegalArgumentException("offset must be >= 0");
}
checkEncodingScheme(enc);
if (contents != null) {
this.content = new byte[length];
System.arraycopy(contents, offset, this.content, 0, length);
}
this.mimeType = mimeType;
this.contentID = contentId;
this.contentLocation = contentLocation;
this.encoding = enc;
| static boolean | containsNonUSASCII(java.lang.String str)Checks if a string contains non-ASCII characters.
int numChars = str.length();
for (int i = 0; i < numChars; ++i) {
char thisChar = str.charAt(i);
if (thisChar < US_ASCII_LOWEST_VALID_CHAR ||
thisChar != (thisChar & US_ASCII_VALID_BIT_MASK))
return true;
}
return false;
| public byte[] | getContent()Returns the content of the MessagePart as an array of
bytes. If it's not possible to create an arary which can contain all
data, this method must throw an OutOfMemoryError .
if (content == null) {
return null;
}
byte[] copyOfContent = new byte[content.length];
System.arraycopy(content, 0, copyOfContent, 0, content.length);
return copyOfContent;
| public java.io.InputStream | getContentAsStream()Returns an InputStream for reading the contents of the
MessagePart . Returns an empty stream if no content is
available.
if (content == null) {
return new ByteArrayInputStream(new byte[0]);
} else {
return new ByteArrayInputStream(content);
}
| public java.lang.String | getContentID()Returns the content-id value of the MessagePart .
return contentID;
| public java.lang.String | getContentLocation()Returns content location of the MessagePart .
return contentLocation;
| public java.lang.String | getEncoding()Returns the encoding of the content, e.g. "US-ASCII", "UTF-8",
"UTF-16", ... as a String .
return encoding;
| public int | getLength()Returns the content size of this MessagePart .
return content == null ? 0 : content.length;
| public java.lang.String | getMIMEType()Returns the mime type of the MessagePart .
return mimeType;
|
|