Server.Network.Compression.Compress C# (CSharp) Méthode

Compress() public static méthode

public static Compress ( byte input, int offset, int count, int &length ) : byte[]
input byte
offset int
count int
length int
Résultat byte[]
		public unsafe static byte[] Compress( byte[] input, int offset, int count, ref int length ) {
			if ( input == null ) {
				throw new ArgumentNullException( "input" );
			} else if ( offset < 0 || offset >= input.Length ) {
				throw new ArgumentOutOfRangeException( "offset" );
			} else if ( count < 0 || count > input.Length ) {
				throw new ArgumentOutOfRangeException( "count" );
			} else if ( ( input.Length - offset ) < count ) {
				throw new ArgumentException();
			}

			length = 0;

			if ( count > DefiniteOverflow ) {
				return null;
			}

			lock ( _syncRoot ) {
				int bitCount = 0;
				int bitValue = 0;

				fixed ( int* pTable = _huffmanTable ) {
					int* pEntry;

					fixed ( byte* pInputBuffer = input ) {
						byte* pInput = pInputBuffer + offset, pInputEnd = pInput + count;

						fixed ( byte* pOutputBuffer = _outputBuffer ) {
							byte* pOutput = pOutputBuffer, pOutputEnd = pOutput + BufferSize;

							while ( pInput < pInputEnd ) {
								pEntry = &pTable[*pInput++ << 1];

								bitCount += pEntry[CountIndex];

								bitValue <<= pEntry[CountIndex];
								bitValue |= pEntry[ValueIndex];

								while ( bitCount >= 8 ) {
									bitCount -= 8;

									if ( pOutput < pOutputEnd ) {
										*pOutput++ = ( byte ) ( bitValue >> bitCount );
									} else {
										return null;
									}
								}
							}

							// terminal code
							pEntry = &pTable[0x200];

							bitCount += pEntry[CountIndex];

							bitValue <<= pEntry[CountIndex];
							bitValue |= pEntry[ValueIndex];

							// align on byte boundary
							if ( ( bitCount & 7 ) != 0 ) {
								bitValue <<= ( 8 - ( bitCount & 7 ) );
								bitCount += ( 8 - ( bitCount & 7 ) );
							}

							while ( bitCount >= 8 ) {
								bitCount -= 8;

								if ( pOutput < pOutputEnd ) {
									*pOutput++ = ( byte ) ( bitValue >> bitCount );
								} else {
									return null;
								}
							}

							length = ( int ) ( pOutput - pOutputBuffer );
							return _outputBuffer;
						}
					}
				}
			}
		}

Same methods

Compression::Compress ( byte input, int length, byte &output, int &outputLength ) : void

Usage Example

Exemple #1
0
        private void InternalCompile(bool compress)
        {
            if (m_Length == 0)
            {
                long streamLen = m_Stream.Length;

                m_Stream.Seek(1, SeekOrigin.Begin);
                m_Stream.Write((ushort)streamLen);
            }
            else if (m_Stream.Length != m_Length)
            {
                int diff = (int)m_Stream.Length - m_Length;

                Console.WriteLine("Packet: 0x{0:X2}: Bad packet length! ({1}{2} bytes)", m_PacketID, diff >= 0 ? "+" : "", diff);
            }

            MemoryStream ms = m_Stream.UnderlyingStream;

            m_CompiledBuffer = ms.GetBuffer();
            int length = (int)ms.Length;

            if (compress)
            {
                try
                {
                    Compression.Compress(m_CompiledBuffer, length, out m_CompiledBuffer, out length);
                }
                catch (IndexOutOfRangeException)
                {
                    Console.WriteLine("Warning: Compression buffer overflowed on packet 0x{0:X2} ('{1}') (length={2})", m_PacketID, GetType().Name, length);

                    m_CompiledBuffer = null;
                }
            }

            if (m_CompiledBuffer != null)
            {
                m_CompiledLength = length;

                byte[] old = m_CompiledBuffer;

                if (length > BufferSize || (m_State & State.Static) != 0)
                {
                    m_CompiledBuffer = new byte[length];
                }
                else
                {
                    m_CompiledBuffer = m_Buffers.AcquireBuffer();
                    m_State         |= State.Buffered;
                }

                Buffer.BlockCopy(old, 0, m_CompiledBuffer, 0, length);
            }

            PacketWriter.ReleaseInstance(m_Stream);
            m_Stream = null;
        }
All Usage Examples Of Server.Network.Compression::Compress