[java] Reading a file thats both ascii and binary

Started by
5 comments, last by CaptainJester 17 years, 8 months ago
Hey I'm currently trying to implement a PLY file importer. That file format starts off with a header in ascii, and then the rest of the file can be in either ascii, binary big endian, or binary little endian. The one where both the header and the data is in ascii I have already implemented but now for the ascii - binary structure. How do I read a file thats first ascii and then binary? What I do now is that I created a BufferedInputStream from the file and then I create one BufferedReader and one DataInputStream from that. If the file is only ascii, I read it through with the reader but when it's a ascii - binary file I read the first part with the reader and then start reading with the DataInputStream. But it seams like the DataInputStream starts from the begining again. Anyhow I end up with the wrong data. Anyone? Lizard
Advertisement
Just read the whole thing as a binary file, and extract the ASCII part yourself. All you have to do is finding the newline characters.
Load it as binary in a ByteArrayInputStream. Then to read the ascii part put the bytes into a ByteArrayOutputStream piped up to a BufferedReader that you can then use to read the ascii parts. Then you can make your descion from there.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Quote:Original post by CaptainJester
Load it as binary in a ByteArrayInputStream. Then to read the ascii part put the bytes into a ByteArrayOutputStream piped up to a BufferedReader that you can then use to read the ascii parts. Then you can make your descion from there.


Hey I can't quite follow what you mean? Could I see some example code? How do you pipe a InputStream to a OutputStream?

Edit: Ok I found PipedReader.
Edit2: Ok I don't understand how the *beep* piped streams could help me??

Lizard

[Edited by - LizardCPP on August 17, 2006 4:25:32 PM]
I have never tried to use a Pipe either.

Here is what I was talking about.
import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.InputStreamReader;public class Test {    public static void main(String[] args) {        try {            //read file and store binary in bout            ByteArrayOutputStream bout = new ByteArrayOutputStream();            InputStream in = Test.class.getResourceAsStream("test.txt");            byte buffer[] = new byte[4096];                        int read = 0;            do {                read = in.read(buffer);                if(read != -1) {                    bout.write(buffer, 0, read);                }            } while(read != -1);                        //copy binary to an input stream for reading and parsing            //save this for multiple uses            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());            BufferedReader bufRead = new BufferedReader(new InputStreamReader(bin));                        String line = "";                        do {                line = bufRead.readLine();                //parse ascii part here                if(line != null) {                    System.out.println(line);                }            } while(line != null);                        //now that you read the ascii part decide what you need to do.            //to read binary, just do reads from bin directly        }        catch(Exception e) {            e.printStackTrace();        }    }    }
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Ok what you show me above is similar to what I did before. I tried your code with a example program and I'm still having problem with this.

This is the example program:
import java.io.*;public class Main{    public Main()    {        try        {            File file  = new File("c:\\dev\\models\\bunny\\BBPbunny.ply");            FileInputStream fin = new FileInputStream(file);            ByteArrayOutputStream bout = new ByteArrayOutputStream();            byte buffer[] = new byte[4096];            int read = 0;            do            {                read = fin.read(buffer);                if(read != -1)                {                    bout.write(buffer, 0, read);                }            }            while(read != -1);            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());            BufferedReader reader = new BufferedReader(new InputStreamReader(bin));            for (int line = 0; line < 9; line++)            {                String lineString = reader.readLine();                System.out.println(lineString);            }            DataInputStream stream = new DataInputStream(bin);            float f = stream.readFloat();            System.out.println(f);        }        catch(IOException e)        {            e.printStackTrace();        }    }    public static void main(String[] args)    {        new Main();    }}


The file I'm using to test this is from here: http://www.cs.ucl.ac.uk/staff/Joao.Oliveira/ply.html

It's the Nbunny.zip. It comes in 12 diffrent versions, 6 ascii and 6 binary. If I read the ascii files the first float I read should be: -0.012775 but the output I get here is:

ply
format binary_big_endian 1.0
element vertex 788
property float x
property float y
property float z
element face 1443
property list uchar int vertex_indices
end_header
0.00249417
I think it is a position thing. Here is what I tried.
        try {            File file = new File("C:\\Test\\Apps\\eclipse\\workspace\\Test\\bin\\BBPbunny.ply");            FileInputStream fin = new FileInputStream(file);            FileChannel fc = fin.getChannel();            ByteBuffer buf = ByteBuffer.allocateDirect((int)fc.size());            fc.read(buf);            byte buffer[] = new byte[(int)fc.size()];            //BIG_ENDIAN is the default            buf.order(ByteOrder.BIG_ENDIAN);            buf.rewind();            buf.get(buffer);            ByteArrayInputStream bin = new ByteArrayInputStream(buffer);            BufferedReader reader = new BufferedReader(new InputStreamReader(bin));            for (int line = 0; line < 9; line++) {                String lineString = reader.readLine();                System.out.println(lineString);            }            DataInputStream stream = new DataInputStream(bin);            buf.position(180);            float f = buf.getFloat();            System.out.println(f);        }        catch (Exception e) {            e.printStackTrace();        }


And my results:
ply
format binary_big_endian 1.0
element vertex 788
property float x
property float y
property float z
element face 1443
property list uchar int vertex_indices
end_header
-0.0127752


Whe you are reading from the DataInputStream, you either are out of position or it is reading in the wrong ENDIANness.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement