Creating a byte array from a stream

How can I do this

I've tried...

Dim AccountToUpload As Byte() = System.Text.Encoding.Default.GetBytes(AccountUploadStream.ReadByte)

...but my result it -1.

The stream is declared as...

Dim AccountUploadStream As System.IO.MemoryStream = New System.IO.MemoryStream

Any ideas The data stored in the stream is an XML document.

Thanks.


Answer this question

Creating a byte array from a stream

  • campwes

    Try this code:

    Module Module1

    Sub Main()

    Dim AccountUploadStream As System.IO.MemoryStream = New System.IO.MemoryStream(New Byte(99) {})

    Dim ByteReader As New IO.BinaryReader(AccountUploadStream)

    Dim AccountToUpload As Byte() = ByteReader.ReadBytes(AccountUploadStream.Length)

    Console.WriteLine(AccountToUpload.Length)

    End Sub

    End Module



  • Joao Gouveia

    No problem - let me try to clear up some more - what is your local (client) machine, and what is your server machine (ftp server)

    how are you actually getting the data into accounttoupload



  • M3

    When you create a new memory stream, you can give it a buffer to initialize it - so I'm declaring a byte array with 100 elements:

    new Byte(99) {}

    a new Byte array with 99 elements - the curly braces mean that you're creating an array, instead of trying to call a constructor and passing it 99.

    You can also write:

    new Byte(){1,2,3,4,5} - this will create a byte array with 5 elements, which hold the values 1 through 5.

    As for point 1, I'm not sure what's going on - I guess it's possible the encoding on the server is different that the local system, or the ftp upload process is mangling the text. A couple of questions, can you open the local file in notepad and read it before sending it Is it after it is uploaded that the file is mangled Is it completely mangled, or is it just the new lines that are broken

    P.S. - I'm really not an ftp expert - not sure if I can really help you with that.



  • FNFCasey

    Hi,

    I'm using a free FTP host for the server machine, and I'm just connecting to it using an FTP component I downloaded.

    We've already worked out that its not the uploading process causing errors, so its either the code that creates a byte array from the stream, or the code that writes the XML file to a stream.
    Here's the code for my entire CreateAccount subroutine:

    Sub CreateAccount(ByVal UserName As String, ByVal Password As String, ByVal AccountLevel As String)

    Dim AccountUploadStream As System.IO.MemoryStream = New System.IO.MemoryStream(New Byte(99) {})

    REM Creates an XMLWriterSettings object to use for setting up the XML file created
    Dim XmlSettings As New System.Xml.XmlWriterSettings

    REM Causes all new XML Elements to be indented by a tab space
    XmlSettings.Indent = True
    XmlSettings.IndentChars = " "

    REM Creates a new XmlWriter object, and writes data passed into the function into a stream
    Using NewAccount As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(AccountUploadStream, XmlSettings)
    With NewAccount
    .WriteStartElement("User")
    .WriteStartElement("UserDetails")
    .WriteElementString("UserName", UserName)
    .WriteElementString("Password", Password)
    .WriteElementString("ShortName", Nothing)
    .WriteEndElement()
    .WriteStartElement("AccountDetails")
    .WriteElementString("AccountStatus", "Enabled")
    .WriteElementString("AccountLevel", AccountLevel)
    .WriteElementString("AccountLastEdited", My.Computer.Clock.GmtTime.ToString)
    .WriteEndElement()
    .WriteEndElement()
    End With
    End Using

    Dim ByteReader As New IO.BinaryReader(AccountUploadStream)
    Dim AccountToUpload As Byte() = ByteReader.ReadBytes(AccountUploadStream.Length)

    Server.LicenseKey = "Secure FTP Factory for .NET:Evaluation:Evaluation:05-12-2006:DMxybO6dbJLNJXDucfCpqoejbiwHVNNPAR0yHWa2RCTotZhpGFnWRbEUZkrZGbiCqWglYqjPA9tOVZpwg7ABxKqaRCgffnArPGXA9m9925EKpo9aotpLPvtQy+Jpc9Tq5IB8ldTbRBe4lNaoAsGNuEQGhBvlMh27OVY2j1rM3mM="
    Server.Connect()
    Server.Upload(AccountToUpload, UserName & ".txt", True)


    End Sub

    Hope this is helpful.

    Thanks.


  • byon

    The buffer size (100 bytes) is too small for what you're trying to write to it - try defining it as being 1000 bytes long, seems to work for me.

  • Wojtek

    Hi,

    I've saved the byte array as a local file...

    My.Computer.FileSystem.WriteAllBytes("C:\TestAccount.txt", AccountToUpload, False)

    ...and the text is still mangled. To be specific, there is data in the file, but Notepad simply displays it as:

    (This is the data):

    Which appears to be a lot of spaces.

    Could it be because the writing and uploading methods read the bytes in reverse order to the desired way

    Sorry for my vagueness, I'm very new to Visual Basic and programming in general!

    Thanks.


  • CSharpNewbie22

    Alex Moura,

    Your code seems to work, but;

    1. The XML file consists of some unreadable characters when opened it Notepad. I needed a byte array to upload the stream to an FTP server, so it may be the FTP code that is working unexpectably so I'll look into that.

    2. New System.IO.MemoryStream(New Byte(99 {})) - in this line, what is the 99 for Also, the parantheses I've never seen anything like this used before.

    Thanks.


  • Creating a byte array from a stream