-2

What I want to do is save 4x8bytes as a 64bit Long. And decode that 64bit Long into 4x8bytes again.

I know you may not understand it but I have an Encoder, which uses bytes 8 bits to make a 64 bit Long. And I'm saving multiple of those an example: "-223784 2432834 -34233566" and I want to read every number split it when " " is the character and put it in a long[].

Currently I have this Code:

FileInputStream fin = new FileInputStream( IOUtils.path + File.separator + "eclipse.hm" );
    String c = "";
    long[] longs = new long[1000000];

    int b,ggg=0;
    while((b=fin.read())!=-1) {
        if( (char)b==' ' ) {
            longs[ggg++] = Long.parseLong(c);
            c = "";
        } else {
            c+=(char) b;
        }
        fetched++;
    }
    fin.close();

The Method of my "Decoder" is as follows:

public static Object decode(long[] input) throws DataFormatException, IOException, ClassNotFoundException {
    byte[] toInflate = BitSet.valueOf(input).toByteArray();

    Inflater inflater = new Inflater();
    inflater.setInput(toInflate);

    byte[] deflated = new byte[ toInflate.length*2 ];
    inflater.inflate(deflated);
    inflater.end();

    ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(deflated) );
    Object r = ois.readObject();
    ois.close();
    return r;
}

The Decoder works I had tested it with my Encoder and directly input the output of the Encoder. So there must be a read error.

and I'm literally speechless, as well as I don't have anything in mind to fix this problem...

Thanks for help, sincerly Richee.

  • What is your question? [Edit] your text and make it clear what you want to do, what you have tried, and what results you get, including unexpected results or error messages. –  Sep 11 '18 at 20:14

2 Answers2

1

You can use some built-in String functions to split string. After that you need to do some transformations from string to long for all elements that you get after split step.

Ekrem Demirhan
  • 204
  • 3
  • 11
  • Do you need look for char by char when read from a file? I edited my answer so you can check if you do any optimization on your own code. I tested it with a nearly 1000000 character long string. – Ekrem Demirhan Sep 11 '18 at 19:10
  • 1
    Thanks now it works, I was sure I did the same thing as well and it did not work... Guess I have just done something else wrong in that method then.. Thanks :) – Richee Nektar Sep 11 '18 at 19:14
  • Problem Idk why but the Stream corrupts – Richee Nektar Sep 11 '18 at 19:33
0

To fix this Issue I tried, removing the Deflater from my Encoder and Decoder(Inflater). After that it worked so now I am wondering why does the Deflater/Inflater "destroy" my StreamHeader....

Anyways sorry for taking your time...