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

Home Posts Topics Members FAQ

Using .Read(buffer, 0, totalSize) with low RAM

89 New Member
I have only 1GB of RAM so I cannot work with files too big with the ReadtoEnd method.
Here is the code Im trying so that the file would split in pieces of 256MB:

Expand|Select|Wrap|Line Numbers
  1.  Try
  2.                Dim BInput As New FileStream(Filename, FileMode.Open, FileAccess.Read)
  3.                Dim Reader As New BinaryReader(BInput)
  4.  
  5.                Dim info As New FileInfo(Filename)
  6.                totalSize = info.Length
  7.                Dim i As Integer = totalSize / Dtblock 'Dtblock is a const 256mb
  8.                Dim completed As Integer = 0
  9.  
  10.                Dim firstround As Boolean = True
  11.  
  12.                If (totalSize > Dtblock) Then
  13.                   'The file is bigger then 256MB
  14.                   'Prepare to split
  15.                   Dim EngineObj As New Engine()
  16.                   ReDim buffer(Dtblock)
  17.  
  18.                   While completed < totalSize
  19.  
  20.                      Reader.Read(buffer, completed, completed + Dtblock)
  21.                      '..
  22. 'here I work with the buffer, then I have to save it
  23.   '..
  24.  
  25.                      completed += Dtblock + 1
  26.  
  27.                      If firstround Then 'here I check if I should create or append to output
  28.                         'Write
  29.                         firstround = False
  30.                         Dim BOutput As New FileStream(TxtBoxOutput.Text, FileMode.Create, FileAccess.Write)
  31.                         Dim writer As New BinaryWriter(BOutput)
  32.                         writer.Write(buffer)
  33.                      Else
  34.                         'Append
  35.                         Dim BOutput As New FileStream(TxtBoxOutput.Text, FileMode.Append, FileAccess.Write)
  36.                         Dim writer As New BinaryWriter(BOutput)
  37.                         writer.Write(buffer)
  38.                      End If
  39.  
  40.                   End While
  41.  
  42.                Else
  43.                   'The file is smaller then 256MB
  44.  
  45.                   ReDim buffer(totalSize)
  46.                   Reader.Read(buffer, 0, totalSize)
  47. ..
  48.                  'Here I work with the buffer..
  49. ..
  50.                   'Write buffer to new file
  51.                   Dim BOutput As New FileStream(TxtBoxOutput.Text, FileMode.Create, FileAccess.Write)
  52.                   Dim writer As New BinaryWriter(BOutput)
  53.                   writer.Write(buffer)
  54.                   'Done, close instances of file streams
  55.                   Reader.Close()
  56.                   writer.Close()
  57.  
  58.                End If
  59.  
  60.                BInput.Dispose()
  61.                MessageBox.Show("Operation finished!")
  62.             Catch ex As Exception
  63.                MessageBox.Show(ex.ToString)
  64.             End Try
I know it might be horrible, but its the only idea I came up with.
anyone knows a better way to accomplish this?

Or if somebody find knows why I always get an exception on the "Reader.Read(buffer, completed, completed + Dtblock)" saying :

"Offset and length for the array lie outside of the valid field, or the number is greater than the number of the elements of the index to at the end of the source listing."

would be even better..

but till now I dont know why that happens because I supposed that the parameters for Read was like this:

Read(<WheretoStore>,<starting point>, <how many to read from starting point>)

correct?
Aug 3 '07 #1
4 2021
TRScheel
638 Recognized Expert Contributor
I am not sure, but I think the error is because when you read, you move the start location of the read. For example:

Expand|Select|Wrap|Line Numbers
  1. The dog jumped over the moon
If you wrote:

Expand|Select|Wrap|Line Numbers
  1. Read(myBuffer, 0, 3)
You would get 'The'

If you did it again, you would get ' do'. But if instead you did:

Expand|Select|Wrap|Line Numbers
  1. Read(myBuffer, 2, 3)
you would get 'og '

This is all purely speculation, a short test should prove this wrong/right.
Aug 3 '07 #2
thiago777
89 New Member
Hey!
thank you very much for your reply.

you were right about the Read's parameter.

I just didnt know that it had an internal pointer that keeps track of the last byte processed ;)

thanks again!

later,
Thiago.
Aug 12 '07 #3
TRScheel
638 Recognized Expert Contributor
No problem, glad to have helped.
Aug 13 '07 #4
Plater
7,872 Recognized Expert Expert
For Read the paremters are:
[number of bytes actually read] Read( [name of byte[] you want the data to go into], [starting index in the GIVEN byte[] (the previous parameter)], [total number of bytes to attempt to read] );

so if I wanted to try to read 1024 bytes into a buffer starting at the first index in my array I woul do this:
Expand|Select|Wrap|Line Numbers
  1. Byte[] buff=new Byte[1024];
  2. int actualread=-1;
  3. actaulread=mystream.Read(buff,0,buff.Length);
  4. //actualread will be set to how many bytes I actually was able to read, or 0 if end of stream
  5.  
Aug 13 '07 #5

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...
1
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've...
5
by: Tim | last post by:
Hi, I'm experiencing some problem with the following code: st = File.Open(sFilename, FileMode.Open, FileAccess.ReadWrite) br = New BinaryReader(st) Do Until br.PeekChar = -1 Dim buffer()...
4
by: David Buchan | last post by:
Hello, I wonder if anyone could help me. I'm using vb.NET and I'd like to read a binary file, byte by byte, and then write to another file (making a duplicate, identical file). I'd then...
0
by: Craig Buchanan | last post by:
when i try to open a file that has been compressed with GZipStream (source from microsoft's 101 Samples for Visual Studio 2005), i get an "invalid archive" message from winzip, zipgenius and xp's...
4
by: chenatcr | last post by:
Hello, I added a serial-USB converter to my laptop, it appear as USB serial port COM4, and I wrote an application to read 78k data from this port. In my VC++ express code, I defined...
0
by: duclm | last post by:
Hi all, II am having problem sending zip file(binary file) using webrequest function from wince5.0 device to a webpage. It works fine for any file with size less than or equal to 1.5kb. Any bigger...
1
by: WeCi2i | last post by:
Okay, I have a problem that has been stumping me for weeks. I have tried many different solutions and this is pretty much my last resort. I have seen a lot of good answers give here so I figured I...
0
by: Lambda | last post by:
I'd like to write compressed data to a file. To improve performance, I want to use buffer. I read some books, they all talk about 'character' stream. But I think compressed data should be...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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.