DataInputStream readShort, readInt...

Runtime

Top Contributor
Hallo Leute,

ich habe da ein kleines Problem:
Ich möchte Werte einlesen mit einem DataInputStream. Wenn ich byte-werte einlese, stimmen sie, aber sobald der Datentyp grösser als 1 byte ist, gibt es ein Fehler:
Java:
    @Override
    public void init() throws Exception {
        DataInputStream in = new DataInputStream(getDataSource().createInputStream());
        System.out.println("available: " + in.available());
        short reserved = in.readShort();
        short type = (short) (in.readShort());// Wert: 256 sollte aber 1 sein -> 256 / 256 = 1
        short count = (short) (in.readShort());// Wert: 512 sollte aber 2 sein -> 512 / 256 = 2
        System.out.println(reserved);
        System.out.println(type);
        System.out.println(count);
        IcoEntry[] entries = new IcoEntry[count];
        for (int i = 0; i < count; i++) {
            entries[i] = new IcoEntry();
            entries[i].read(in);
            entries[i].print();
        }
    }

    private static class IcoEntry {

        byte width;
        byte height;
        byte colorCount;
        byte reserved;
        short planes;
        short bitCount;
        int bytesInResource;
        int offset;

        void read(DataInputStream in) throws Exception {
            width = in.readByte();
            height = in.readByte();
            colorCount = in.readByte();
            reserved = in.readByte();
            planes = in.readShort();
            bitCount = in.readShort();
            bytesInResource = in.readInt();
            offset = in.readInt();
        }

        void print() {
            System.out.println("--------");
            System.out.println("width: " + width);
            System.out.println("height: " + height);
            System.out.println("colorCount: " + colorCount);
            System.out.println("reserved: " + reserved);
            System.out.println("colorPlanes: " + planes);
            System.out.println("bitCount: " + bitCount);
            System.out.println("bytesInResource: " + bytesInResource);
            System.out.println("offset: " + offset);
        }
    }
}

Was mache ich falsch bzw. was mache ich nicht?
 

musiKk

Top Contributor
Und den Fehler sollen wir jetzt erraten?

Kann es sein, dass die Datei mit Little-Endian abgespeichert ist? Java IO ist Big-Endian.
 

Runtime

Top Contributor
Keine Ahnung, noch nie gehört. Wie könnte man den Little-Endian laden?

edit:
Ok, war Little-Endian, Danke!
 
Zuletzt bearbeitet:

Ähnliche Java Themen


Oben