替代com.google.common.io.ByteArrayDataInput吗? [英] Alternative to com.google.common.io.ByteArrayDataInput?

查看:77
本文介绍了替代com.google.common.io.ByteArrayDataInput吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在代码中使用com.google.common.io.ByteArrayDataInput.我正在写的是一种二进制文件的加载器.不幸的是,ByteArrayDataInput不提供其内部字节数组中剩余多少字节的信息.除了此类以外,是否有提供此类信息的替代方法?我知道我可以为com.google.common.io.ByteArrayDataInput编写自己的包装器类,但是我想避免这种情况.

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.

编辑

问题是对我的com.google.common.io.ByteArrayDataInput实例的引用通过一系列方法非常深入地传递.看起来像这样:

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)调用loadB(in),后者又调用loadC(in);,依此类推.

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

每个loadX()方法都会从in中读取某些内容.我想在loadC()中调用ByteArrayDataInput.readFully()方法,并且我想在该级别检查还剩下多少字节要读取.为此,我需要通过所有loadX()方法传递bytesLeft信息.

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.

推荐答案

我必须为番石榴ByteArrayDataInputWrapper准备自己的包装器类. 它添加了一个新的方法getAvailable(),该方法返回要读取的剩余字节数.

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;
    }

}

这篇关于替代com.google.common.io.ByteArrayDataInput吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆