Search code examples
javastringlong-integerdecoder

How to change Longs saved in a String back to Longs


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.


Solution

  • 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.