Code:
/ 4.0 / 4.0 / untmp / 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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ReadOnlyHierarchicalDataSource.cs
- DynamicQueryStringParameter.cs
- HeaderElement.cs
- RepeaterItemCollection.cs
- DataTableNewRowEvent.cs
- NetNamedPipeBindingElement.cs
- ListViewItem.cs
- FlowDocumentScrollViewer.cs
- TableParaClient.cs
- unsafenativemethodsother.cs
- Stackframe.cs
- IntersectQueryOperator.cs
- CodeDefaultValueExpression.cs
- ListParagraph.cs
- SponsorHelper.cs
- FigureHelper.cs
- IISMapPath.cs
- DataMemberFieldEditor.cs
- TransformerInfo.cs
- Soap.cs
- CacheDict.cs
- XmlILConstructAnalyzer.cs
- EntityProviderServices.cs
- ParentQuery.cs
- DataGridViewHitTestInfo.cs
- PermissionListSet.cs
- InputMethod.cs
- AvtEvent.cs
- XmlConvert.cs
- SetterTriggerConditionValueConverter.cs
- TypeSystem.cs
- ResourceExpression.cs
- ChtmlSelectionListAdapter.cs
- RetriableClipboard.cs
- AssemblyInfo.cs
- DataListDesigner.cs
- PieceDirectory.cs
- WorkflowInstanceExtensionCollection.cs
- Pkcs9Attribute.cs
- AsyncOperation.cs
- OutputWindow.cs
- BinaryReader.cs
- safelink.cs
- RoleManagerEventArgs.cs
- NamedPipeProcessProtocolHandler.cs
- TextLine.cs
- ColumnCollection.cs
- SeekStoryboard.cs
- XmlReader.cs
- ConfigXmlDocument.cs
- KeysConverter.cs
- XmlWrappingReader.cs
- _CookieModule.cs
- TriggerCollection.cs
- SerializationInfoEnumerator.cs
- Function.cs
- ConcatQueryOperator.cs
- AttachedPropertyBrowsableForTypeAttribute.cs
- GridViewItemAutomationPeer.cs
- SiteOfOriginPart.cs
- FixedHyperLink.cs
- DataRowIndexBuffer.cs
- WebPartConnectVerb.cs
- DrawingGroupDrawingContext.cs
- TypeKeyValue.cs
- ListViewGroup.cs
- ToolStripDropDownMenu.cs
- PropertyState.cs
- IsolatedStorage.cs
- XmlSchemaSimpleTypeRestriction.cs
- MatchSingleFxEngineOpcode.cs
- Literal.cs
- DataGridViewColumnHeaderCell.cs
- PropertyPathConverter.cs
- _SecureChannel.cs
- TableRow.cs
- XmlMapping.cs
- ResXResourceSet.cs
- PowerModeChangedEventArgs.cs
- KeyPressEvent.cs
- WebPartDeleteVerb.cs
- DeviceContexts.cs
- ObjectStateEntryOriginalDbUpdatableDataRecord.cs
- DynamicHyperLink.cs
- FlowLayout.cs
- ControlEvent.cs
- NamedObject.cs
- TextAction.cs
- HuffModule.cs
- CombinedGeometry.cs
- SafeRightsManagementPubHandle.cs
- CompositeCollection.cs
- CallbackTimeoutsBehavior.cs
- Sequence.cs
- EventWaitHandle.cs
- IPHostEntry.cs
- ArgIterator.cs
- ModelItemDictionary.cs
- ActivityStatusChangeEventArgs.cs
- ToolStripSystemRenderer.cs