Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. deflateStream reading from stream and writing to a buffer

deflateStream reading from stream and writing to a buffer

Scheduled Pinned Locked Moved Visual Basic
visual-studiocom
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ed Hill _5_
    wrote on last edited by
    #1

    I'm having problems understanding why a loop is needed to read from a deflated stream. i've replaced the read all bytes in stream method with the following line, however the data in the output buffer is 1 byte smaller than it was before the data was compressed: Return stream.Read(buffer, 0, buffer.Length) used to be: Dim offset As Integer = 0 Dim totalCount As Integer = 0 While True Dim bytesRead As Integer = stream.Read(buffer, offset, 100) If bytesRead = 0 Then Exit While End If offset += bytesRead totalCount += bytesRead End While Return totalCount the full code for this is taken from http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream(VS.80).aspx[^]

    G 1 Reply Last reply
    0
    • E Ed Hill _5_

      I'm having problems understanding why a loop is needed to read from a deflated stream. i've replaced the read all bytes in stream method with the following line, however the data in the output buffer is 1 byte smaller than it was before the data was compressed: Return stream.Read(buffer, 0, buffer.Length) used to be: Dim offset As Integer = 0 Dim totalCount As Integer = 0 While True Dim bytesRead As Integer = stream.Read(buffer, offset, 100) If bytesRead = 0 Then Exit While End If offset += bytesRead totalCount += bytesRead End While Return totalCount the full code for this is taken from http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream(VS.80).aspx[^]

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      The Read method doesn't have to read all data that you request. It returns the number of bytes actually read, so you have to loop until you have filled the buffer or until the method returns zero.

      Dim offset As Integer = 0
      Dim bytesRead As Integer
      Do
      bytesRead = stream.Read(buffer, offset, buffer.Length - offset)
      offset += bytesRead
      Loop Until bytesRead = 0 Or offset = buffer.Length
      Return offset

      Experience is the sum of all the mistakes you have done.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups