| 55 | } |
| 56 | |
| 57 | U32 SFXMemoryStream::read( U8* buffer, U32 length ) |
| 58 | { |
| 59 | U32 bufferOffset = 0; |
| 60 | |
| 61 | // Determine how much we're supposed to read. |
| 62 | |
| 63 | U32 numBytesToCopy = length; |
| 64 | if( mNumSamplesLeft != U32_MAX ) |
| 65 | numBytesToCopy = getMin( length, mNumSamplesLeft * mFormat.getBytesPerSample() ); |
| 66 | numBytesToCopy -= numBytesToCopy % mFormat.getBytesPerSample(); |
| 67 | |
| 68 | // Copy the data. |
| 69 | |
| 70 | U32 numBytesLeftToCopy = numBytesToCopy; |
| 71 | while( numBytesLeftToCopy ) |
| 72 | { |
| 73 | // If we have a current packet, use its data. |
| 74 | |
| 75 | if( mCurrentPacket ) |
| 76 | { |
| 77 | U32 numBytesLeftInCurrentPacket = mCurrentPacket->size - mCurrentPacketOffset; |
| 78 | |
| 79 | // Copy data. |
| 80 | |
| 81 | if( numBytesLeftInCurrentPacket ) |
| 82 | { |
| 83 | const U32 remainingNumBytesToCopy = getMin( numBytesLeftInCurrentPacket, numBytesLeftToCopy ); |
| 84 | dMemcpy( &buffer[ bufferOffset ], &mCurrentPacket->data[ mCurrentPacketOffset ], remainingNumBytesToCopy); |
| 85 | |
| 86 | bufferOffset += remainingNumBytesToCopy; |
| 87 | mCurrentPacketOffset += remainingNumBytesToCopy; |
| 88 | numBytesLeftInCurrentPacket -= remainingNumBytesToCopy; |
| 89 | numBytesLeftToCopy -= remainingNumBytesToCopy; |
| 90 | } |
| 91 | |
| 92 | // Discard the packet if there's no data left. |
| 93 | |
| 94 | if( !numBytesLeftInCurrentPacket ) |
| 95 | { |
| 96 | destructSingle( mCurrentPacket ); |
| 97 | mCurrentPacket = NULL; |
| 98 | mCurrentPacketOffset = 0; |
| 99 | } |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | // Read a new packet. |
| 104 | |
| 105 | if( !getSourceStream()->read( &mCurrentPacket, 1 ) ) |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Update count of remaining samples. |
| 111 | |
| 112 | U32 numBytesCopied = numBytesToCopy - numBytesLeftToCopy; |
| 113 | if( mNumSamplesLeft != U32_MAX ) |
| 114 | mNumSamplesLeft -= ( numBytesCopied / mFormat.getBytesPerSample() ); |
no test coverage detected