Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a wcf rest service, which I created a MemoryStream for one of my operation contracts that has an object, which encapsulates many properties. This is a GET HTTP request for data to the client (Android music app).

I've wrote some code to take my service side MemoryStream into the java based application, but I am not quite sure how I supposed to manage the conversion of the MemoryStream to a InputStreamArray. Two different languages (c# & java). Can anybody help out with some code example. I will post my code base if anyone wants to see it, but it pretty complex to try arranging it here and making good sence to others.

Here below is my output of my wcf message, which is envolope into a JSON format. Anybody help will be greatly appretiated. Thanks!

Here is my wcf web servie JSON format before processing my generic List Object into a MemoryStream.

JavaScript
LoadTracksByAlbumResult: [{AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg",…},…]
2.	0: {AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg",…}
1.	AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg"
2.	AlbumID: 2135
3.	AlbumName: "Sauce Walka - The Sauce Father"
4.	Artists: "(Sauce Walka . )"
5.	CreatedBy: 0
6.	CreatedOn: "6/5/2017"
7.	DiscJockeyName: "(No DJ. )"
8.	DjID: 23
9.	DownloadStatus: false
10.	TotalStreams: "1039"
11.	TrackDescription: "Description 1"
12.	TrackID: 35094
13.	TrackStatus: true
14.	TrackTitle: "01-Sauce_Walka-We_Did_It"
15.	TrackUrl: "/UploadContents/AlbumTracks/2135/01-Sauce_Walka-We_Did_It.mp3"
16.	UpdatedBy: 0
17.	UpdatedOn: ""
3.	1: {AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg",…}
4.	2: {AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg",…}
5.	3: {AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg",…}
6.	4: {AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg",…}
7.	5: {AlbumCoverPhotoUrl: "/UploadContents/AlbumCoverPhoto/2135/Sauce Walka - The Sauce Father.jpg",…}


And here below is my wcf web servie JSON format wrapped into a MemoryStream.

What I have tried:

JavaScript
{LoadTracksByAlbumResult: {__identity: null,…}}
	LoadTracksByAlbumResult: {__identity: null,…}
1.	__identity: null
2.	_buffer: [91, 123, 34, 84, 114, 97, 99, 107, 73, 68, 34, 58, 51, 53, 48, 57, 52, 44, 34, 84, 114, 97, 99, 107,…]
3.	_capacity: 7522
4.	_expandable: false
5.	_exposable: false
6.	_isOpen: true
7.	_length: 7522
8.	_origin: 0
9.	_position: 0
10.	_writable: true


Partial code from the server side of the memorystream below. Prepares a Data object for transmission over the wire.


Function implementation below:
Java



Java
byte[] byteArray = new byte[4000];

           // Convert the data Object (generic list) to be serialized and return a JSON string.
           var json1 = new JavaScriptSerializer().Serialize(loadAlbumTracksList1);

           // Convert the string into bytes of array.
           byteArray = Encoding.UTF8.GetBytes(json1);

           //public override bool
           MemoryStream stream = new MemoryStream(byteArray);
           //stream.GetBuffer();

           return stream;


Client side java: Consuming for chuncks of data, so that performance is good for listening to mp3 files off the server. This is partial code below.

The String responsible parameter is where the memoryStream enter after it goes through the main Network (url, hostname, endpoint, port # and etc. are process here) of the application. Below is a aditional network for the stream process only, even though I wrote the code to accept 2 other wcf web service calls, which are coming from the server buffered. Can you please assist? I know I receive an error saying that the charset is not mappable.

Java
public String getStream(String responseBody) {
        String result = "";
        InputStream inputStream = null;

        try {
            /*
             * Encodes this {@code String} into a sequence of bytes using the
             * platform's default charset, storing the result into a new byte array.
             */
            inputStream = new ByteArrayInputStream(responseBody.getBytes(), 0, 0);

            result = convertInputStreamToString(inputStream);

        }
        catch(Exception ex)
        {
            Log.e("Exception",ex.getLocalizedMessage());
        }
        finally{
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


Java
private static String convertInputStreamToString(InputStream inputStream) throws IOException {

        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream) );
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)// Reads characters into a portion of an array.
            result += line;

        inputStream.close();
        return result;

    }


Java
@Override
    public void onRequestSuccess(String responseBody, String type) {
        // Call the getStream function to convert streamToString
        NetworkClient streamProcess = new NetworkClient();

        String streamResponse = streamProcess.getStream(responseBody);
        JSONObject obj = CommonServices.stringToJSON(streamResponse);
        if(obj != null){
            switch (type) {
                case "loadtrack":
                    loadGridFromSavedJSONData(obj);
Posted
Updated 15-Jan-20 11:13am
v2

A MemoryStream is just a stream, i.e. a list of byte values. You have to send the byte array over the wire (either directly by writing from memory stream to a network stream, or by encoding it, for example in Base64, and send the encoded value instead). In the receiving java application, you then either read the network stream directly, or decode the Base64 message to its original byte-array values.

WCF: Large Data and Streaming[^]
Java 8 - Streams - Tutorialspoint[^]
 
Share this answer
 
Comments
Dalerico 15-Jan-20 16:39pm    
Thank you very much sir for your response. I pretty much I believe have done exactly what you mentioned in your post, but I'm having issues decoding the UTF-8 (Base64) back into characters. It's really strange to me that I have 2 additional wcf services (buffered type) requests come through this same entry point and they were encoded and decoded with no issues. I've tried so many different senarios, I can't even decode the buffered data through stream network part of my application. I should of saved the orginal code, because it also attempted to decode the stream as well, but failed to keep the data decoded. Can you look at code snipplet of mines where the stream is coming into the functioning and basically being converting from a string (memoryStream wrapped into a json format) and passed into a inputStreamArray constructor, to be process for decoding?
phil.o 15-Jan-20 16:54pm    
Please use the green Improve question widget which appears on hovering your question, and add the snippets which deal with encoding and deconding parts. Please, do not write your code block in a comment; comments are not meant to display code blocks properly.
Partial code (c#) from the server side: Prepares a Data object for transmission over the wire

C#
byte[] byteArray = new byte[4000];

           // Convert the data Object (generic list) to be serialized and return a JSON string.
           var json1 = new JavaScriptSerializer().Serialize(loadAlbumTracksList1);

           // Convert the string into bytes of array.
           byteArray = Encoding.UTF8.GetBytes(json1);

           //public override bool
           MemoryStream stream = new MemoryStream(byteArray);
           //stream.GetBuffer();

           return stream;


Client side java: Consuming for chuncks of data, so that performance is good for listening to mp3 files off the server. This is partial code below.

The String responsible parameter is where the memoryStream enter after it goes through the main Network (url, hostname, endpoint, port # and etc. are process here) of the application. Below is a aditional network for the stream process only, even though I wrote the code to accept 2 other wcf web service calls, which are coming from the server buffered. Can you please assist? I know I receive an error saying that the charset is not mappable.

Java
public String getStream(String responseBody) {
        String result = "";
        InputStream inputStream = null;

        try {
            /*
             * Encodes this {@code String} into a sequence of bytes using the
             * platform's default charset, storing the result into a new byte array.
             */
            inputStream = new ByteArrayInputStream(responseBody.getBytes(), 0, 0);

            result = convertInputStreamToString(inputStream);

        }
        catch(Exception ex)
        {
            Log.e("Exception",ex.getLocalizedMessage());
        }
        finally{
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


Java
private static String convertInputStreamToString(InputStream inputStream) throws IOException {

        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream) );
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)// Reads characters into a portion of an array.
            result += line;

        inputStream.close();
        return result;

    }


Java
@Override
    public void onRequestSuccess(String responseBody, String type) {
        // Call the getStream function to convert streamToString
        NetworkClient streamProcess = new NetworkClient();

        String streamResponse = streamProcess.getStream(responseBody);
        JSONObject obj = CommonServices.stringToJSON(streamResponse);
        if(obj != null){
            switch (type) {
                case "loadtrack":
                    loadGridFromSavedJSONData(obj);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900