locked
How can I convert stream to byte and byte to stream

    Question

  • I got a IInputStream from BackgroundDownloader operation, how can I convert it to byte array? I try to convert it to buffer and after that to bytes, but it's always throw exception.

     function decryptStream(inputStream, totalBytes) {
           
                    var reader = new Windows.Storage.Streams.DataReader(inputStream);
                    var iBuffer = reader.readBuffer(totalBytes);
                    var bytes = Windows.Security.Cryptography.CryptographicBuffer.copyToByteArray(iBuffer);
                   
            }
    

    Could I have examples about that?

    Wednesday, November 02, 2011 7:32 AM

Answers

  • Hello Vivian,

    You should be able to do so something similar to this:

    reader.loadAsync(size).then(
       function (readsize) {
            var array = new Array(readsize);
            reader.readBytes(array);

     

    -Jeff


    Jeff Sanders (MSFT)
    • Marked as answer by Vivian Ng Friday, November 04, 2011 2:36 AM
    Thursday, November 03, 2011 5:49 PM
    Moderator

All replies

  • Hi Vivian,

    Why are you using Crypto functions in your program?  Is there additional code around this that requires it?  What is the exception you are getting, any inner exceptions and what line of code is this happening on?

    -Jeff


    Jeff Sanders (MSFT)
    Wednesday, November 02, 2011 1:30 PM
    Moderator
  • Hi Jeft,

    I am trying to convert stream to byte array, but I don't see any method support. So that I try using Crypto to test. The exception I got is "range is not valid" at reader.readBuffer(totalByes). Do we have any examples of convert stream to byte array?

    Vivian Ng

    Thursday, November 03, 2011 6:58 AM
  • Hi Jeft,

    In my program, I have a BackgroundDownloader to download an image. When we have result, I will convert it to bytes array.

    if (p.hasResponseChanged && imageStream == null) {
                            
                            imageStream = operation.getResultStreamAt(0);  
    } 
                        if (!p.hasResponseChanged && imageStream != null) {
                           
                            decryptStream(imageStream, p['bytesReceived']);
    }


    Thursday, November 03, 2011 7:07 AM
  • Hello Vivian,

    You should be able to do so something similar to this:

    reader.loadAsync(size).then(
       function (readsize) {
            var array = new Array(readsize);
            reader.readBytes(array);

     

    -Jeff


    Jeff Sanders (MSFT)
    • Marked as answer by Vivian Ng Friday, November 04, 2011 2:36 AM
    Thursday, November 03, 2011 5:49 PM
    Moderator
  • Hi Jeft,

    I got it. Thank you very much.

    Vivian

    Friday, November 04, 2011 2:35 AM
  • From winrt.d.ts, DataReader::readBytes does not take any parameter unlike specified above. During runtime, execution throws exception that 0x800a13ee - Javascript runtime  error:readBytes: function called with too few arguments. Has there been any changes since the original answer was posted ?
    Friday, February 20, 2015 12:26 AM