Alternative to com.google.common.io.ByteArrayDataInput?

只谈情不闲聊 提交于 2019-12-24 15:17:43

问题


I'm currently using com.google.common.io.ByteArrayDataInput in my code. What I'm writing is a sort of loader for a binary file. Unfortunately, ByteArrayDataInput does not provide the information how many bytes left in its internal byte array. Is there any alternative to this class that provides such infomation? I know I can write my own wrapper class for com.google.common.io.ByteArrayDataInput, but I'd like to avoid that.

EDIT

The problem is that the reference to my com.google.common.io.ByteArrayDataInput instance is passed very deeply through a chain of methods. It looks like this:

loadA(ByteArrayDataInput in) invokes loadB(in) which invokes loadC(in); and so forth..

every of loadX() methods reads something from in. And I would like to invoke ByteArrayDataInput.readFully() method in loadC() and I would like to check at that level how many bytes left to read. To achieve that with this class I would need to pass bytesLeft info through all the loadX() methods.


回答1:


ByteArrayInputStream's available method returns the number of bytes left in the array according to the source. Wrapping that in an ObjectInputStream would seem to give you just about the same functionality. ObjectInputStream's available() method returns available() from the underlying input stream without doing any processing.




回答2:


I had to prepare my own wrapper class for Guava ByteArrayDataInputWrapper. It adds a new method getAvailable() which returns the number of bytes left to read.

public class ByteArrayDataInputWrapper implements ByteArrayDataInput {

    private static final int INT_LENGTH = 4;
    private static final int FLOAT_LENGTH = 4;  
    private static final int LONG_LENGTH = 8;
    private static final int DOUBLE_LENGTH = 8; 

    private ByteArrayDataInput in;
    private int available;

    public ByteArrayDataInputWrapper(byte[] binary) {
        if (binary != null) {
            in = ByteStreams.newDataInput(binary);
            available = binary.length;
        }
    }

    public int getAvailable() {
        return available;
    }   

    @Override
    public void readFully(byte[] b) {
        in.readFully(b);
        available -= b.length;
    }

    @Override
    public void readFully(byte[] b, int off, int len) {
        in.readFully(b, off, len);
        available -= len;
    }

    @Override
    public int skipBytes(int n) {
        int skipped = in.skipBytes(n);
        available -= skipped;
        return skipped;
    }

    @Override
    public boolean readBoolean() {
        boolean result = in.readBoolean();
        available--;
        return result;
    }

    @Override
    public byte readByte() {
        byte result = in.readByte();
        available--;
        return result;
    }

    @Override
    public int readUnsignedByte() {
        int result = in.readUnsignedByte();
        available--;
        return result;
    }

    @Override
    public short readShort() { 
        short result = in.readShort(); 
        available -= 2;
        return result;
    }

    @Override
    public int readUnsignedShort() {
        int result = in.readUnsignedShort();
        available -= 2;
        return result;
    }

    @Override
    public char readChar() {
        char result = in.readChar();
        available -= 2;
        return result;
    }

    @Override
    public int readInt() {
        int result = in.readInt();
        available -= INT_LENGTH;
        return result;
    }

    @Override
    public long readLong() {
        long result = in.readLong();
        available -= LONG_LENGTH;
        return result;
    }

    @Override
    public float readFloat() {
        float result = in.readFloat();
        available -= FLOAT_LENGTH;
        return result;
    }

    @Override
    public double readDouble() {
        double result = in.readDouble();
        available -= DOUBLE_LENGTH;
        return result;
    }

    @Override
    public String readLine() {
        String result = in.readLine();
        if (result != null) {
            available -= result.length();
        }
        return result;
    }

    @Override
    public String readUTF() {
        String result = in.readUTF();
        if (result != null) {
            available -= result.length();
        }
        return result;
    }

}


来源:https://stackoverflow.com/questions/9378862/alternative-to-com-google-common-io-bytearraydatainput

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!