FileDocCategorySizeDatePackage
MessagePart.javaAPI DocphoneME MR2 API (J2ME)8434Wed May 02 18:00:44 BST 2007javax.wireless.messaging

MessagePart

public class MessagePart extends Object
This class is defined by the JSR-205 specification Wireless Messaging API (WMA) Version 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)

        construct(contents, offset, length, mimeType, contentId,
            contentLocation, enc);
    
public MessagePart(byte[] contents, String mimeType, String contentId, String contentLocation, String enc)

        construct(contents, 0, (contents == null ? 0 : contents.length),
            mimeType, contentId, contentLocation, enc);
    
public MessagePart(InputStream is, String mimeType, String contentId, String contentLocation, String enc)

    
    // JAVADOC COMMENT ELIDED
         
           
             
        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)

 // 30K
    // JAVADOC COMMENT ELIDED
           
           
            
         

        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()

        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()

        if (content == null) {
            return new ByteArrayInputStream(new byte[0]);
        } else {
            return new ByteArrayInputStream(content);
        }
    
public java.lang.StringgetContentID()

        return contentID;
    
public java.lang.StringgetContentLocation()

        return contentLocation;
    
public java.lang.StringgetEncoding()

        return encoding;
    
public intgetLength()

        return content == null ? 0 : content.length; 
    
public java.lang.StringgetMIMEType()

        return mimeType;