Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Main
- Dim rand As New Random
- Dim data = Enumerable.Range(0, 60000000).Select(Function(d) rand.Next(Short.MinValue, Short.MaxValue))
- Dim pth As String = IO.Path.GetTempFileName
- pth.Dump
- Using stream As New FileStream(pth, FileMode.OpenOrCreate)
- Using writer As New BinaryWriter(stream)
- For Each datum In data
- writer.Write(datum)
- Next
- End Using
- End Using
- Dim pth2 As String = IO.Path.GetTempFileName
- pth2.Dump
- Using mem As New IO.MemoryStream
- Using input As New IO.FileStream(pth, FileMode.Open, FileAccess.Read)
- input.CopyTo(mem)
- End Using
- Using output As New IO.MemoryStream 'IO.FileStream(pth2, FileMode.OpenOrCreate, FileAccess.Write)
- Dim convertor As New Uint16StreamConverter(mem, output)
- convertor.ConvertStream
- End Using
- End Using
- End Sub
- Class Uint16StreamConverter
- Private rdr As binaryreader
- Private wrt As binarywriter
- Readonly Property Input As Stream
- ReadOnly Property Output As Stream
- Private converted As Boolean
- Public Sub New(InputStream As Stream, OutputStream As stream)
- 'null checks would be a good idea here
- Input = InputStream
- Output = OutputStream
- rdr = New binaryreader(input, Encoding.GetEncoding("iso-8859-1"))
- wrt = New binarywriter(output)
- End Sub
- Sub ConvertStream
- If converted Then Throw New InvalidOperationException("Streams have already been processed.")
- Dim cntr As Long
- Dim sw As New Stopwatch
- sw.Start
- While rdr.PeekChar > -1
- Dim value As Short = rdr.ReadInt16
- cntr += 1
- Dim newValue As UInt16
- 'newValue = ...
- wrt.Write(newValue)
- If cntr Mod 1000 = 0 Then
- 'Console.WriteLine($"Read {cntr} values")
- End If
- End While
- sw.Stop
- sw.Dump("Time to convert")
- converted = True
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement