FileDocCategorySizeDatePackage
SmsSplitter.javaAPI DocAndroid 1.5 API3781Wed May 06 22:42:46 BST 2009com.android.im.imps

SmsSplitter

public class SmsSplitter extends Object
A helper class to split the payload into several segments to meet the size constraint of the sms.

Fields Summary
private static final int
MAX_SEGMENT_COUNT
private ByteBuffer
mOutBuffer
private int
mMaxSegmentLen
private byte[]
mData
private int
mPreambleEnd
private int
mCurrentSegment
private int
mSegmentCount
Constructors Summary
public SmsSplitter(int maxLen)


       
        mMaxSegmentLen = maxLen;
        mOutBuffer = ByteBuffer.allocate(maxLen);
    
Methods Summary
private voidcalculateSegments()

        int totalLen = mData.length;
        if (totalLen < mMaxSegmentLen) {
            mSegmentCount = 1;
        } else {
            searchPreambleEnd();
            int newPreambleLen = mPreambleEnd + 2;
            int segmentPayload = mMaxSegmentLen - newPreambleLen - 1;
            int totalPayload = totalLen - mPreambleEnd - 1;
            mSegmentCount = (totalPayload + segmentPayload -1) / segmentPayload;
        }
    
public byte[]getNext()
Gets the next segment.

return
The next segment.
throws
IndexOutOfBoundsException

        if (mCurrentSegment >= mSegmentCount) {
            throw new IndexOutOfBoundsException();
        }
        byte[] segment;
        if (mSegmentCount == 1) {
            segment = mData;
        } else {
            mOutBuffer.clear();
            // The original preamble
            mOutBuffer.put(mData, 0, mPreambleEnd);
            // Two character of DD
            mOutBuffer.put((byte) ('a" + mCurrentSegment));
            mOutBuffer.put((byte) ('a" + mSegmentCount - 1));
            // The space after preamble
            mOutBuffer.put((byte) ' ");

            // The payload
            int segmentPayload = mMaxSegmentLen - mPreambleEnd - 3;
            int offset = mPreambleEnd + 1 + segmentPayload * mCurrentSegment;
            int len = (offset + segmentPayload > mData.length) ?
                    mData.length - offset : segmentPayload;
            mOutBuffer.put(mData, offset, len);

            mOutBuffer.flip();
            segment = new byte[mOutBuffer.limit()];
            mOutBuffer.get(segment);
        }
        mCurrentSegment++;
        return segment;
    
public booleanhasNext()

        return mCurrentSegment < mSegmentCount;
    
private voidsearchPreambleEnd()

        byte[] data = mData;
        int index = 0;
        while(index < data.length && data[index] != ' ") {
            index++;
        }
        mPreambleEnd = index;
    
public intsplit(byte[] data)
Split the data into several segments to meet the size constraint.

param
data The data to split. MUST be a valid PTS primitive.
return
The count of segments of the result or -1 if the data is too long.

        mData = data;
        mCurrentSegment = 0;
        calculateSegments();
        if (mSegmentCount > MAX_SEGMENT_COUNT) {
            mSegmentCount = -1;
        }
        return mSegmentCount;