FileDocCategorySizeDatePackage
MessagePart.javaAPI DocphoneME MR2 API (J2ME)17848Wed May 02 18:00:44 BST 2007com.sun.tck.wma

MessagePart

public 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.
since
WMA 2.0

Fields Summary
static int
MAX_PART_SIZE_BYTES
Maximum size for message part.
static final int
BUFFER_SIZE
Buffer size 2048.
byte[]
content
Content byte array.
String
contentID
MIME Content ID.
String
contentLocation
Content location.
String
encoding
Content encoding.
String
mimeType
MIME type.
static final char
US_ASCII_LOWEST_VALID_CHAR
Lowest valid ASCII character.
static final char
US_ASCII_VALID_BIT_MASK
Mask 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.

param
contents byte array containing the contents for the MessagePart.
param
offset start position
param
length the number of bytes to be included in the MessagePart.
param
mimeType the MIME Content-Type for the MessagePart [RFC 2046]
param
contentId the content-id header field value for the MessagePart [RFC 2045]. The content-id is unique over all MessageParts of a MultipartMessage and must always be set for each message part.
param
contentLocation the content location which specifies the file name of the file that is attached. If the content location is set to null no content location will be set for this MessagePart.
param
enc the encoding scheme for the MessagePart. if enc is set to null no encoding will be used for this MessagePart.
throws
java.lang.IllegalArgumentException if mimeType or contentId is null. This exception will be thrown if contentID or contentLocation contains other characters than specified in US-ASCII format. This exception will be thrown if either length is less than 0 or offset + length exceeds the length of the content or if offset is less than 0 or if the specified encoding scheme is unknown.
throws
SizeExceededException if the contents is larger than the available memory or supported size for the message part

        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.

param
contents byte array containing the contents for the MessagePart. The contents of the array will be copied into the MessagePart.
param
mimeType the MIME Content-Type for the MessagePart [RFC 2046]
param
contentId the content-id header field value for the MessagePart [RFC 2045]. The content-id is unique over all MessageParts of a MultipartMessage and must always be set for each message part.
param
contentLocation the content location which specifies the file name of the file that is attached. If the content location is set to null no content location will be set for this MessagePart.
param
enc the encoding scheme for the MessagePart. if enc is set to null no encoding will be used for this MessagePart.
throws
java.lang.IllegalArgumentException if mimeType or contentId is null. This exception will be thrown if contentID or contentLocation contains other characters than specified in US-ASCII format or if the specified encoding scheme is unknown.
throws
SizeExceededException if the contents is larger than the available memory or supported size for the message part

        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.

param
is InputStream from which the contents of the MessagePart are read.
param
mimeType the MIME Content-Type for the MessagePart [RFC 2046]
param
contentId the content-id header field value for the MessagePart [RFC 2045]. The content-id is unique over all MessageParts of a MultipartMessage and must always be set for each message part.
param
contentLocation the content location which specifies the file name of the file that is attached. If the content location is set to null no content location will be set for this MessagePart.
param
enc the encoding scheme for the MessagePart. if enc is set to null no encoding will be used for this MessagePart.
throws
java.io.IOException if reading the InputStream causes an exception other than EOFException.
throws
java.lang.IllegalArgumentException if mimeType or contentId is null. This exception will be thrown if contentID or contentLocation contains other characters than specified in US-ASCII format or if the specified encoding scheme is unknown.
throws
SizeExceededException of the content from the InputStream is larger than the available memory or supported size for the message part.

    
                                                                                                                                                                                                                                                                                                                   
         
           
             
        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 voidcheckContentID(java.lang.String contentId)
Verifies the content identifier.

param
contentId content id to be checked
exception
IllegalArgumentException if content id is not valid

        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 voidcheckContentLocation(java.lang.String contentLoc)
Verifies the content location.

param
contentLoc content location to be checked.
exception
IllegalArgumentException if content location is not valid.

        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 voidcheckEncodingScheme(java.lang.String encoding)
Verifies the content encoding.

param
encoding The content encoding to be checked.
exception
IllegalArgumentException if content encoding is not valid.

        // IMPL_NOTE: check for a valid encoding scheme        
    
voidconstruct(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.

param
contents byte array containing the contents for the MessagePart.
param
offset start position
param
length the number of bytes to be included in the MessagePart.
param
mimeType the MIME Content-Type for the MessagePart [RFC 2046]
param
contentId the content-id header field value for the MessagePart [RFC 2045]. The content-id is unique over all MessageParts of a MultipartMessage and must always be set for each message part.
param
contentLocation the content location which specifies the file name of the file that is attached. If the content location is set to null no content location will be set for this MessagePart.
param
enc the encoding scheme for the MessagePart. if enc is set to null no encoding will be used for this MessagePart.
throws
java.lang.IllegalArgumentException if mimeType or contentId is null. This exception will be thrown if contentID or contentLocation contains other characters than specified in US-ASCII format. This exception will be thrown if either length is less than 0 or offset + length exceeds the length of the content or if offset is less than 0 or if the specified encoding scheme is unknown.
throws
SizeExceededException if the contents is larger than the available memory or supported size for the message part

 // 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 booleancontainsNonUSASCII(java.lang.String str)
Checks if a string contains non-ASCII characters.

param
str Text to be checked.
return
true if non-ASCII characters are found.

    
                                 
        
        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.

return
MessagePart data as byte array

        if (content == null) {
            return null;
        }
        byte[] copyOfContent = new byte[content.length];
        System.arraycopy(content, 0, copyOfContent, 0, content.length);
        return copyOfContent;
    
public java.io.InputStreamgetContentAsStream()
Returns an InputStream for reading the contents of the MessagePart. Returns an empty stream if no content is available.

return
an InputStream that can be used for reading the contents of this MessagePart.

        if (content == null) {
            return new ByteArrayInputStream(new byte[0]);
        } else {
            return new ByteArrayInputStream(content);
        }
    
public java.lang.StringgetContentID()
Returns the content-id value of the MessagePart.

return
the value of the content-id as a String, or null if the content-id is not set (possible if the message was sent from a not JSR 205 compliant client).

        return contentID;
    
public java.lang.StringgetContentLocation()
Returns content location of the MessagePart.

return
content location

        return contentLocation;
    
public java.lang.StringgetEncoding()
Returns the encoding of the content, e.g. "US-ASCII", "UTF-8", "UTF-16", ... as a String.

return
the encoding of the MessagePart content or null if the encoding scheme of the MessagePart cannot be determined.

        return encoding;
    
public intgetLength()
Returns the content size of this MessagePart.

return
Content size (in bytes) of this MessagePart or 0 if the MessagePart is empty.

        return content == null ? 0 : content.length; 
    
public java.lang.StringgetMIMEType()
Returns the mime type of the MessagePart.

return
MIME type of the MessagePart.

        return mimeType;