Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Sys / System / IO / compression / OutputBuffer.cs / 1305376 / OutputBuffer.cs
namespace System.IO.Compression { using System.Diagnostics; internal class OutputBuffer { private byte[] byteBuffer; // buffer for storing bytes private int pos; // position private uint bitBuf; // store uncomplete bits private int bitCount; // number of bits in bitBuffer // set the output buffer we will be using internal void UpdateBuffer(byte[] output) { byteBuffer = output; pos = 0; } internal int BytesWritten { get { return pos; } } internal int FreeBytes { get { return byteBuffer.Length - pos; } } internal void WriteUInt16(ushort value) { Debug.Assert(FreeBytes >= 2, "No enough space in output buffer!"); byteBuffer[pos++] = (byte)value; byteBuffer[pos++] = (byte)(value >> 8); } internal void WriteBits(int n, uint bits) { Debug.Assert(n <= 16, "length must be larger than 16!"); bitBuf |= bits << bitCount; bitCount += n; if (bitCount >= 16) { Debug.Assert(byteBuffer.Length - pos >= 2, "No enough space in output buffer!"); byteBuffer[pos++] = unchecked((byte)bitBuf); byteBuffer[pos++] = unchecked((byte)(bitBuf >> 8)); bitCount -= 16; bitBuf >>= 16; } } // write the bits left in the output as bytes. internal void FlushBits() { // flush bits from bit buffer to output buffer while (bitCount >= 8) { byteBuffer[pos++] = unchecked((byte)bitBuf); bitCount -= 8; bitBuf >>= 8; } if (bitCount > 0) { byteBuffer[pos++] = unchecked((byte)bitBuf); bitBuf = 0; bitCount = 0; } } internal void WriteBytes(byte[] byteArray, int offset, int count) { Debug.Assert(FreeBytes >= count, "Not enough space in output buffer!"); // faster if (bitCount == 0) { Array.Copy(byteArray, offset, byteBuffer, pos, count); pos += count; } else { WriteBytesUnaligned(byteArray, offset, count); } } private void WriteBytesUnaligned(byte[] byteArray, int offset, int count) { for (int i = 0; i < count; i++) { byte b = byteArray[offset + i]; WriteByteUnaligned(b); } } private void WriteByteUnaligned(byte b) { WriteBits(8, b); } internal int BitsInBuffer { get { return (bitCount / 8) + 1; } } internal OutputBuffer.BufferState DumpState() { OutputBuffer.BufferState savedState; savedState.pos = pos; savedState.bitBuf = bitBuf; savedState.bitCount = bitCount; return savedState; } internal void RestoreState(OutputBuffer.BufferState state) { pos = state.pos; bitBuf = state.bitBuf; bitCount = state.bitCount; } internal struct BufferState { internal int pos; // position internal uint bitBuf; // store uncomplete bits internal int bitCount; // number of bits in bitBuffer } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. namespace System.IO.Compression { using System.Diagnostics; internal class OutputBuffer { private byte[] byteBuffer; // buffer for storing bytes private int pos; // position private uint bitBuf; // store uncomplete bits private int bitCount; // number of bits in bitBuffer // set the output buffer we will be using internal void UpdateBuffer(byte[] output) { byteBuffer = output; pos = 0; } internal int BytesWritten { get { return pos; } } internal int FreeBytes { get { return byteBuffer.Length - pos; } } internal void WriteUInt16(ushort value) { Debug.Assert(FreeBytes >= 2, "No enough space in output buffer!"); byteBuffer[pos++] = (byte)value; byteBuffer[pos++] = (byte)(value >> 8); } internal void WriteBits(int n, uint bits) { Debug.Assert(n <= 16, "length must be larger than 16!"); bitBuf |= bits << bitCount; bitCount += n; if (bitCount >= 16) { Debug.Assert(byteBuffer.Length - pos >= 2, "No enough space in output buffer!"); byteBuffer[pos++] = unchecked((byte)bitBuf); byteBuffer[pos++] = unchecked((byte)(bitBuf >> 8)); bitCount -= 16; bitBuf >>= 16; } } // write the bits left in the output as bytes. internal void FlushBits() { // flush bits from bit buffer to output buffer while (bitCount >= 8) { byteBuffer[pos++] = unchecked((byte)bitBuf); bitCount -= 8; bitBuf >>= 8; } if (bitCount > 0) { byteBuffer[pos++] = unchecked((byte)bitBuf); bitBuf = 0; bitCount = 0; } } internal void WriteBytes(byte[] byteArray, int offset, int count) { Debug.Assert(FreeBytes >= count, "Not enough space in output buffer!"); // faster if (bitCount == 0) { Array.Copy(byteArray, offset, byteBuffer, pos, count); pos += count; } else { WriteBytesUnaligned(byteArray, offset, count); } } private void WriteBytesUnaligned(byte[] byteArray, int offset, int count) { for (int i = 0; i < count; i++) { byte b = byteArray[offset + i]; WriteByteUnaligned(b); } } private void WriteByteUnaligned(byte b) { WriteBits(8, b); } internal int BitsInBuffer { get { return (bitCount / 8) + 1; } } internal OutputBuffer.BufferState DumpState() { OutputBuffer.BufferState savedState; savedState.pos = pos; savedState.bitBuf = bitBuf; savedState.bitCount = bitCount; return savedState; } internal void RestoreState(OutputBuffer.BufferState state) { pos = state.pos; bitBuf = state.bitBuf; bitCount = state.bitCount; } internal struct BufferState { internal int pos; // position internal uint bitBuf; // store uncomplete bits internal int bitCount; // number of bits in bitBuffer } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- GetReadStreamResult.cs
- AutomationPatternInfo.cs
- HttpHeaderCollection.cs
- NumericPagerField.cs
- ScalarConstant.cs
- HtmlInputSubmit.cs
- RawStylusInputReport.cs
- GenericWebPart.cs
- MDIWindowDialog.cs
- ELinqQueryState.cs
- Helpers.cs
- MsmqVerifier.cs
- _WinHttpWebProxyDataBuilder.cs
- IsolatedStorageFilePermission.cs
- ConnectionManagementElement.cs
- WmlTextViewAdapter.cs
- FontFamily.cs
- PipelineDeploymentState.cs
- DBCommandBuilder.cs
- OleDbCommandBuilder.cs
- hresults.cs
- CriticalFinalizerObject.cs
- VersionedStreamOwner.cs
- COM2Properties.cs
- SessionEndedEventArgs.cs
- DataRowChangeEvent.cs
- DataGridColumnCollectionEditor.cs
- Vector3DValueSerializer.cs
- CharacterMetrics.cs
- DefaultSerializationProviderAttribute.cs
- CodeMemberField.cs
- BitmapMetadataEnumerator.cs
- ChildTable.cs
- XmlElementElementCollection.cs
- CodeConditionStatement.cs
- DiscoveryDocumentLinksPattern.cs
- LineMetrics.cs
- TransformerConfigurationWizardBase.cs
- HtmlInputCheckBox.cs
- XamlParser.cs
- Pool.cs
- ImageListStreamer.cs
- clipboard.cs
- NativeWindow.cs
- Component.cs
- HandledEventArgs.cs
- StaticResourceExtension.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- PolygonHotSpot.cs
- GeometryConverter.cs
- wgx_exports.cs
- WebPart.cs
- SchemaTableOptionalColumn.cs
- ThicknessAnimationUsingKeyFrames.cs
- DeferredBinaryDeserializerExtension.cs
- ProxyGenerationError.cs
- EdmToObjectNamespaceMap.cs
- PaintEvent.cs
- XmlQueryRuntime.cs
- ProjectionPruner.cs
- EventlogProvider.cs
- GregorianCalendar.cs
- PtsCache.cs
- BufferModeSettings.cs
- RuntimeResourceSet.cs
- ColumnWidthChangingEvent.cs
- WSHttpBindingElement.cs
- errorpatternmatcher.cs
- ImageButton.cs
- ListDesigner.cs
- ResourceType.cs
- ActivityCodeDomSerializationManager.cs
- PageTheme.cs
- DbConvert.cs
- TextDpi.cs
- Internal.cs
- WinEventHandler.cs
- RadioButton.cs
- SourceItem.cs
- PrintDialog.cs
- SspiNegotiationTokenAuthenticatorState.cs
- PolyLineSegmentFigureLogic.cs
- _BasicClient.cs
- ResourceAttributes.cs
- PointConverter.cs
- XmlSchemaSimpleContentRestriction.cs
- PropertyTabAttribute.cs
- EncryptedKey.cs
- RegisteredArrayDeclaration.cs
- EnumBuilder.cs
- DataSourceDesigner.cs
- FileEnumerator.cs
- PolicyAssertionCollection.cs
- IChannel.cs
- TreeNodeEventArgs.cs
- HwndMouseInputProvider.cs
- SecondaryViewProvider.cs
- DesignerForm.cs
- OleDbStruct.cs
- ParallelDesigner.cs