/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This implementation only supports sync base streams. Verify this in debug // builds. Note, this can be simpler than the check in // SnappyUncompressInputStream because we don't have to deal with the // nsStringInputStream oddness of being non-blocking and sync. #ifdef DEBUG bool baseNonBlocking;
nsresult rv = mBaseStream->IsNonBlocking(&baseNonBlocking);
MOZ_ASSERT(NS_SUCCEEDED(rv));
MOZ_ASSERT(!baseNonBlocking); #endif
}
if (!mBaseStream) { return NS_BASE_STREAM_CLOSED;
}
if (!mBuffer) {
mBuffer.reset(new (fallible) char[mBlockSize]); if (NS_WARN_IF(!mBuffer)) { return NS_ERROR_OUT_OF_MEMORY;
}
}
while (aCount > 0) { // Determine how much space is left in our flat, uncompressed buffer.
MOZ_ASSERT(mNextByte <= mBlockSize);
uint32_t remaining = mBlockSize - mNextByte;
// If it is full, then compress and flush the data to the base stream. if (remaining == 0) {
nsresult rv = FlushToBaseStream(); if (NS_WARN_IF(NS_FAILED(rv))) { return rv;
}
// Now the entire buffer should be available for copying.
MOZ_ASSERT(!mNextByte);
remaining = mBlockSize;
}
// Lazily create the compressed buffer on our first flush. This // allows us to report OOM during stream operation. This buffer // will then get re-used until the stream is closed. if (!mCompressedBuffer) {
mCompressedBufferLength = MaxCompressedBufferLength(mBlockSize);
mCompressedBuffer.reset(new (fallible) char[mCompressedBufferLength]); if (NS_WARN_IF(!mCompressedBuffer)) { return NS_ERROR_OUT_OF_MEMORY;
}
}
// The first chunk must be a StreamIdentifier chunk. Write it out // if we have not done so already.
nsresult rv = MaybeFlushStreamIdentifier(); if (NS_WARN_IF(NS_FAILED(rv))) { return rv;
}
// Compress the data to our internal compressed buffer.
size_t compressedLength;
rv = WriteCompressedData(mCompressedBuffer.get(), mCompressedBufferLength,
mBuffer.get(), mNextByte, &compressedLength); if (NS_WARN_IF(NS_FAILED(rv))) { return rv;
}
MOZ_ASSERT(compressedLength > 0);
mNextByte = 0;
// Write the compressed buffer out to the base stream.
uint32_t numWritten = 0;
rv = WriteAll(mCompressedBuffer.get(), compressedLength, &numWritten); if (NS_WARN_IF(NS_FAILED(rv))) { return rv;
}
MOZ_ASSERT(compressedLength == numWritten);
// Build the StreamIdentifier in our compressed buffer.
size_t compressedLength;
nsresult rv = WriteStreamIdentifier(
mCompressedBuffer.get(), mCompressedBufferLength, &compressedLength); if (NS_WARN_IF(NS_FAILED(rv))) { return rv;
}
// Write the compressed buffer out to the base stream.
uint32_t numWritten = 0;
rv = WriteAll(mCompressedBuffer.get(), compressedLength, &numWritten); if (NS_WARN_IF(NS_FAILED(rv))) { return rv;
}
MOZ_ASSERT(compressedLength == numWritten);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.