GZipStream.Read GZipStream.Read GZipStream.Read GZipStream.Read Method

Definition

Overloads

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

Reads a sequence of bytes from the current GZip stream into a byte span and advances the position within the GZip stream by the number of bytes read.

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

Reads a number of decompressed bytes into the specified byte array.

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

Reads a sequence of bytes from the current GZip stream into a byte span and advances the position within the GZip stream by the number of bytes read.

public:
 override int Read(Span<System::Byte> buffer);
public override int Read (Span<byte> buffer);
override this.Read : Span<byte> -> int
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

Parameters

buffer
Span<Byte>

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.

Returns

The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Remarks

Use the CanRead property to determine whether the current instance supports reading. Use the ReadAsync method to read asynchronously from the current stream.

This method read a maximum of buffer.Length bytes from the current stream and store them in buffer. The current position within the GZip stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the GZip stream remains unchanged. This method will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file). The method is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

Reads a number of decompressed bytes into the specified byte array.

public:
 override int Read(cli::array <System::Byte> ^ array, int offset, int count);
public override int Read (byte[] array, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer

Parameters

array
Byte[]

The array used to store decompressed bytes.

offset
Int32 Int32 Int32 Int32

The byte offset in array at which the read bytes will be placed.

count
Int32 Int32 Int32 Int32

The maximum number of decompressed bytes to read.

Returns

The number of bytes that were decompressed into the byte array. If the end of the stream has been reached, zero or the number of bytes read is returned.

Exceptions

The CompressionMode value was Compress when the object was created.

-or-

The underlying stream does not support reading.

offset or count is less than zero.

-or-

array length minus the index starting point is less than count.

Examples

The following example shows how to compress and decompress bytes by using the Read and Write methods.

using System;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace ExampleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            UnicodeEncoding uniEncode = new UnicodeEncoding();

            byte[] bytesToCompress = uniEncode.GetBytes("example text to compress and decompress");
            Console.WriteLine("starting with: " + uniEncode.GetString(bytesToCompress));

            using (FileStream fileToCompress = File.Create("examplefile.gz"))
            {
                using (GZipStream compressionStream = new GZipStream(fileToCompress, CompressionMode.Compress))
                {
                    compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length);
                }
            }

            byte[] decompressedBytes = new byte[bytesToCompress.Length];
            using (FileStream fileToDecompress = File.Open("examplefile.gz", FileMode.Open))
            {
                using (GZipStream decompressionStream = new GZipStream(fileToDecompress, CompressionMode.Decompress))
                {
                    decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length);
                }
            }
            
            Console.WriteLine("ending with: " + uniEncode.GetString(decompressedBytes));
        }
    }
}
Imports System.IO
Imports System.IO.Compression
Imports System.Text

Module Module1

    Sub Main()
        Dim uniEncode As UnicodeEncoding = New UnicodeEncoding()

        Dim bytesToCompress = uniEncode.GetBytes("example text to compress and decompress")
        Console.WriteLine("starting with: " + uniEncode.GetString(bytesToCompress))

        Using fileToCompress As FileStream = File.Create("examplefile.gz")
            Using compressionStream As GZipStream = New GZipStream(fileToCompress, CompressionMode.Compress)
                compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length)
            End Using
        End Using

        Dim decompressedBytes(bytesToCompress.Length - 1) As Byte
        Using fileToDecompress As FileStream = File.Open("examplefile.gz", FileMode.Open)
            Using decompressionStream As GZipStream = New GZipStream(fileToDecompress, CompressionMode.Decompress)
                decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length)
            End Using
        End Using

        Console.WriteLine("ending with: " + uniEncode.GetString(decompressedBytes))
    End Sub
End Module

Remarks

If data is found in an invalid format, an InvalidDataException is thrown as one of the last operations. A cyclic redundancy check (CRC) is performed as one of the last operations of this method.

Applies to