1

Currently working on making a pipeline among two files in java and I would to transmit a float via stream bytes. However I don't know how I can receive it and convert it into a float. Here is what I have done so far:

(3 files)

Consumi.java:

package tryout5_stream_bytes;

import java.io.Serializable;

public class Consumi implements Serializable{

    private float consumi = 0.0F;

    public Consumi(float consumi){
        this.consumi = consumi;
    }

    public float getConsumi(){
        return consumi;
    }

    public byte[] getBytes(String encode){
        return String.valueOf(consumi).getBytes();
    }
}

SimulaConsumi.java

package tryout5_stream_bytes;

import java.io.PipedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.atomic.AtomicBoolean;

public class SimulaConsumi implements Runnable {
    private AtomicBoolean isRunning = new AtomicBoolean(false);
    private PipedOutputStream pos = null;

    public SimulaConsumi(PipedOutputStream pos){
        this.pos = pos;
    }

    @Override
    public void run(){
        isRunning.set(true);

        while(isRunning.get()){
            Consumi c = new Consumi((float) (30 * Math.random()));

            byte[] message = null;
            message = c.getBytes("UTF-8");
            
            try{
                pos.write(message);
                pos.flush();
            } catch(IOException e){
                e.printStackTrace();
            }

            try{
                Thread.sleep(1000);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    public void terminaSimulaConsumi(){
        isRunning.set(false);
    }

}

Main.java

package tryout5_stream_bytes;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.io.*;
import java.lang.object;
import java.nio.ByteBuffer;

public class Main {
    
    public static void main(String[] args){
        PipedInputStream pis = new PipedInputStream();
        PipedOutputStream pos = null;

        try{
            pos = new PipedOutputStream(pis);
        }catch(IOException e){
            e.printStackTrace();
        }

        SimulaConsumi sc = new SimulaConsumi(pos);
        Thread tsc = new Thread();
        tsc.start();

        while(true){
            try{
                Thread.sleep(900);
            }catch(InterruptedException e){
                e.printStackTrace();
            }

            byte[] buffer = new byte[256];
            try{
                pis.read(buffer);
            }catch(IOException e){
                e.printStackTrace();
            }

            float received = //Get a float from a stream bytes???
            System.out.println("Value:"+received);
        }
    }
}

I believe that the sending of the float in the file "SimulaConsumi" is done well (however I might still be wrong). On the other hand I really have no idea how I can receive it!

  • 1
    It looks like your thread does nothing? Don't you have to construct it with `sc` as argument? `Thread tsc = new Thread(sc);` Also, are you required to transfer things as strings? If not you could just use an `ObjectOutputStream` and `ObjectInputStream` on top of the pip streams and exchange instances of class `Float`. To convert the bytes to `float` in your case: first create a string (use constructor that takes a byte array) and then call `Float.parseFloat()` – Daniel Junglas May 20 '22 at 12:27
  • Well, in principle you "just" have to do the steps you did to write the bytes, in opposite direction - i.e. create a String from the bytes, and then convert that to float. I'd recommend to implement your serialization/deserialization logic single-threaded first, and once you know you can convert to your binary format and back again, add things like multiple threads. – Hulk May 20 '22 at 12:32
  • @DanielJunglas I indeed forgot to add sc in the thread tsc, my mistake. I did not really understand how I would be able to use and implement the Float.parseFloat() methods? – Luca Girotti May 20 '22 at 12:33
  • First of all, if you really want to read this from a byte array, make sure to use the return value of [`InputStream.read`](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/io/InputStream.html#read(byte%5B%5D)) - this tells you how may bytes have been read, and you need this information to know which part of your buffer is filled with the bytes you need to convert to `String`. Then, you can build a String from that - https://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and-vice-versa – Hulk May 20 '22 at 12:38
  • @Hulk ok ok, I may have figured something out, I really much appreciate your help! – Luca Girotti May 20 '22 at 12:41
  • I do not recommend using strings or serialization for this. Just wrap your piped streams in [DataInputStream](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/io/DataInputStream.html) and [DataOutputStream](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/io/DataOutputStream.html). – VGR May 20 '22 at 18:56

0 Answers0