codice:
'Copia il contenuto di InStream in OutStream.
'Importante: la gestione degli errori è totalmente a carico del chiamante.
'Gli stream restano aperti al termine della funzione
Public Sub StreamToStream(ByVal InStream As Stream, ByVal OutStream As Stream, Optional ByVal BufSize As Integer = 4096)
Dim readBytes As Integer
If BufSize < 1 Then
Throw New ArgumentOutOfRangeException("BufSize", BufSize, "The buffer size must be greater than zero.")
End If
Dim buffer(BufSize) As Byte
While True
readBytes = InStream.Read(buffer, 0, BufSize)
If readBytes = 0 Then Exit While
OutStream.Write(buffer, 0, readBytes)
End While
End Sub
Esempio di utilizzo:
codice:
Dim InStream As FileStream, OutStream As FileStream
Try
InStream = New FileStream("d:\FileDiInput.bmp", FileMode.Open, FileAccess.Read)
OutStream = New FileStream("d:\FileDiOutput.bmp", FileMode.OpenOrCreate, FileAccess.Write)
StreamToStream(InStream, OutStream)
Catch ex As Exception
'TODO: aggiungere la gestione degli errori
Finally
If Not (InStream Is Nothing) Then
InStream.Close()
End If
If Not (OutStream Is Nothing) Then
OutStream.Close()
End If
End Try