Receive-Stream (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1688"></script>download | new post
A very simple stream-reader implementation (with no error handling) suitable for simple interactive script task …
- function Receive-Stream {
- #.Synopsis
- # Read a stream to the end and close it
- #.Description
- # Will read from a byte stream and output a string or a file
- #.Param reader
- # The stream to read from
- #.Param fileName
- # A file to write to. If this parameter is provided, the function will output to file
- #.Param encoding
- # The text encoding, if you want to override the default.
- param( [System.IO.Stream]$reader, $fileName, $encoding = [System.Text.Encoding]::GetEncoding( $null ) )
- if($fileName) {
- $writer = new-object System.IO.FileStream $fileName, "Create"
- } else {
- [string]$output = ""
- }
- [byte[]]$buffer = new-object byte[] 4096
- [int]$total = [int]$count = 0
- do
- {
- $count = $reader.Read($buffer, 0, $buffer.Length)
- if($fileName) {
- $writer.Write($buffer, 0, $count)
- } else {
- $output += $encoding.GetString($buffer, 0, $count)
- }
- } while ($count -gt 0)
- $reader.Close()
- if(!$fileName) { $output }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository