-1

I need to send a string to a .Net program from a Java program.

I want to take a string from Java and compress it using GZIPOutputStream and decompress it with System.IO.Compression.GZipStream from .Net.

    ByteArrayOutputStream out = new ByteArrayOutputStream();
        try
        {
            GZIPOutputStream gzipOutputStream = new GZIPOutputStream(out);
            gzipOutputStream.write(text.getBytes("utf-8"));
        }
        catch (IOException e)
        {
            //Something
        }

    return out.toByteArray();

Currently I have it so that it returns a byte[] from the Java Program. But is there a way to make byte[] into a String and send the string to .Net? and Be able to turn the String back to byte[] in .Net to decompress it back to a string?

What are my options here? My main goal is to send a compressed string to .Net program. Is that not possible with compressed?

Thanks.

user1542422
  • 309
  • 1
  • 4
  • 13
  • 1
    C# would be able to accept the byte[], but in the first place how would you send it ? Web API ? App-to-App messages ? Service ? – MoustafaS Jun 23 '16 at 21:34
  • 1
    *"But is there a way to make byte[] into a String and send the string to .Net? and Be able to turn the String back to byte[] in .Net to decompress it back to a string?"* You have it the wrong way around: Take a Java string, serialize it to `byte[]` in a well-supported encoding (say, UTF-8), send those bytes to the C# program, and have it deserialize using the same well-supported encoding. If you want to send it compressed, use a well-known compression like gzip and do String > byte[] > gzip => gunzip -> byte[] -> String. – T.J. Crowder Jun 23 '16 at 21:38
  • If you yet need to find a way to communicate between programs I would recommend memory mapped files. [Java](http://howtodoinjava.com/java-7/nio/java-nio-2-0-memory-mapped-files-mappedbytebuffer-tutorial/) [C#](http://www.c-sharpcorner.com/UploadFile/b942f9/using-memory-mapped-files/). – Measurity Jun 23 '16 at 21:41

1 Answers1

0

I think what you may be looking for are named pipes. They're especially designed for inter-process communication.

Since I'm not an expert in Java here is an example on how the Java part is done: SO

The author (v01ver) also links to his website where he describes the C#/.NET part.

However, he/she is not using threads in his C# sample and it should be noted that the pipeServer.WaitForConnection(); method blocks the executing thread.

This MSDN page gives an good example on how to use named pipes with threads (except I would set the IsBackground property of the thread to true to prevent the application from running in the background after the main thread is closed).

Then you could use a callback method or events to process the received data.

If you're required to use gzip compression you can wrap the NamedPipeServerStream in a GZipStream and wrap that in a StreamWriter or StreamReader like so:

        using (var pipeServer = new NamedPipeServerStream("pipename", PipeDirection.InOut))
        using (var gZipDecompressor = new GZipStream(pipeServer, CompressionMode.Decompress))
        using (var gZipCompressor = new GZipStream(pipeServer, CompressionMode.Compress))
        using (var reader = new StreamReader(gZipDecompressor))
        using (var writer = new StreamWriter(gZipCompressor))
        {
            // Use the writer and reader to write and receive strings
        }
Community
  • 1
  • 1
Julian A
  • 334
  • 2
  • 9