473,466 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to read a stream and save it as byte array

24 New Member
Hello everyone,

I try to read a stream and save it as byte array to add it later to zip file.
this is the code that I use
Expand|Select|Wrap|Line Numbers
  1. Dim totalbytes As Byte() = Nothing
  2. Dim iBytesRead As Integer
  3. Dim sChunks As FileStream = File.OpenRead("c:\myfile.wav")
  4. Dim Totalread As Int64 = 0
  5. ReDim totalbytes(sChunks.Length)
  6. Do
  7. iBytesRead = sChunks.Read(totalbytes, Totalread, Totalread+ 1048576)
  8. Totalread+= iBytesRead
  9. Loop While Not iBytesRead = 0
  10. sChunks.Close()
  11. Return totalbytes
  12.  
but there is something is not correct,
Can you please help me in that.

Thank you
Jun 25 '09 #1
3 7364
Curtis Rutland
3,256 Recognized Expert Specialist
Here's how I do my reading in C# :
Expand|Select|Wrap|Line Numbers
  1. FileStream fs = File.OpenRead(filePath);
  2. byte[] buffer = new byte[fs.Length];
  3. fs.Read(buffer, 0, (int)fs.Length);
  4. fs.Flush();
  5. fs.Close();

So I would suggest cutting out your lines 6 through 9 and replacing them with something like this:
Expand|Select|Wrap|Line Numbers
  1. sChunks.Read(totalbytes, 0, totalbytes.Length)
You might have to cast totalbytes.Length from an Int 64 to an Int 32, but I forget how to cast in VB.NET.

Hope that helps.

[EDIT]
For future reference, "there is something is not correct" is not a good enough explanation. You need to tell us what it is doing wrong, and what, if any, exceptions you are getting. Please be more descriptive in the future.
Jun 25 '09 #2
hdbbdh
24 New Member
Thank you for reply

Actually I can read all bytes by File.ReadAllBytes but I need to read this file with progress.
If I read it byte by byte it will take a long time (it is take more than 5 min to read 30MB).
So I think about this way, but there is something wrong, I dont know what is it.

Finally, I am sorry about my English.

Thanks
Jun 25 '09 #3
hdbbdh
24 New Member
Also I found this sample on internet, but because I dont know c# then I dont understand it.
Attached Files
File Type: zip AsyncStream.zip (37.0 KB, 168 views)
Jun 25 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brian | last post by:
I'm having intermittent trouble with a call to the Read method of the HttpWebResponse object. I get an ArumentOutOfRangeException claiming that deep down inside of the Read method, the count...
0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
5
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
1
by: tim | last post by:
I have a multi-dimensional byte array, private someArray(,,) as byte and it's huge redim someArray(100,640,480,3) after populating the array, I write it to a text file to save it. (so I don't...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.