Methods Summary |
---|
private void | calculateSegments()
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.
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 boolean | hasNext()
return mCurrentSegment < mSegmentCount;
|
private void | searchPreambleEnd()
byte[] data = mData;
int index = 0;
while(index < data.length && data[index] != ' ") {
index++;
}
mPreambleEnd = index;
|
public int | split(byte[] data)Split the data into several segments to meet the size constraint.
mData = data;
mCurrentSegment = 0;
calculateSegments();
if (mSegmentCount > MAX_SEGMENT_COUNT) {
mSegmentCount = -1;
}
return mSegmentCount;
|