Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.78 KB | None | 0 0
  1. Sub Main
  2.     Dim rand As New Random
  3.     Dim data = Enumerable.Range(0, 60000000).Select(Function(d) rand.Next(Short.MinValue, Short.MaxValue))
  4.     Dim pth As String = IO.Path.GetTempFileName
  5.     pth.Dump
  6.     Using stream As New FileStream(pth, FileMode.OpenOrCreate)
  7.         Using writer As New BinaryWriter(stream)
  8.             For Each datum In data
  9.                 writer.Write(datum)
  10.             Next
  11.  
  12.         End Using
  13.     End Using
  14.     Dim pth2 As String = IO.Path.GetTempFileName
  15.     pth2.Dump
  16.     Using mem As New IO.MemoryStream
  17.         Using input As New IO.FileStream(pth, FileMode.Open, FileAccess.Read)
  18.             input.CopyTo(mem)
  19.         End Using
  20.         Using output As New IO.MemoryStream 'IO.FileStream(pth2, FileMode.OpenOrCreate, FileAccess.Write)
  21.  
  22.             Dim convertor As New Uint16StreamConverter(mem, output)
  23.             convertor.ConvertStream
  24.         End Using
  25.     End Using
  26.  
  27.  
  28.  
  29.  
  30.  
  31. End Sub
  32.  
  33. Class Uint16StreamConverter
  34.     Private rdr As binaryreader
  35.     Private wrt As binarywriter
  36.     Readonly Property Input As Stream
  37.     ReadOnly Property Output As Stream
  38.     Private converted As Boolean
  39.     Public Sub New(InputStream As Stream, OutputStream As stream)
  40.         'null checks would be a good idea here
  41.         Input = InputStream
  42.         Output = OutputStream
  43.         rdr = New binaryreader(input, Encoding.GetEncoding("iso-8859-1"))
  44.         wrt = New binarywriter(output)
  45.     End Sub
  46.     Sub ConvertStream
  47.         If converted Then Throw New InvalidOperationException("Streams have already been processed.")
  48.         Dim cntr As Long
  49.         Dim sw As New Stopwatch
  50.         sw.Start
  51.         While rdr.PeekChar > -1
  52.             Dim value As Short = rdr.ReadInt16
  53.             cntr += 1
  54.  
  55.  
  56.             Dim newValue As UInt16
  57.             'newValue = ...
  58.  
  59.             wrt.Write(newValue)
  60.             If cntr Mod 1000 = 0 Then
  61.                 'Console.WriteLine($"Read {cntr} values")
  62.             End If
  63.  
  64.         End While
  65.         sw.Stop
  66.         sw.Dump("Time to convert")
  67.  
  68.  
  69.  
  70.         converted = True
  71.  
  72.     End Sub
  73.  
  74.  
  75. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement