当前位置: 首页 > 面试题库 >

Java:以字节顺序替换DataInputStream

微生承业
2023-03-14
问题内容

下面是我的代码,该代码代替了DataInputStream来包装InputStream,但除了读取大字节序类型的常规方法之外,还提供了额外的方法来读取小字节序数据类型。随意使用它。

我有以下几点保留意见。请注意不会更改功能的方法(读取大端类型的功能)。我无法将DataInputStream实现为基类并使用其方法,如read(),readInt(),readChar()等吗?

我的班级层次结构在这里似乎有些奇怪。这样合适吗

这些其他类型(如readUTF()或readLine())是否需要使用小端版本?还是这取决于特定程序?

Java如何存储布尔类型?这是否也符合字节顺序?

感谢您满足我的好奇心:)

import java.io.*;

/**
 * Replacement for a DataInputStream that provides both little and big endian reading capabilities for convenience without need to implement a ByteBuffer
 * @author Bill (unspecified.specification@gmail.com)
 */
public class EndianInputStream extends InputStream implements DataInput {
    private DataInputStream dataInStream;
    private InputStream inStream;
    private byte byteBuffer[];

    /**
     * Constructor to wrap InputStream for little and big endian data
     * @param refInStream Inputstream to wrap
     */
    public EndianInputStream(InputStream refInStream) {
        inStream = refInStream;
        dataInStream = new DataInputStream(inStream);
        byteBuffer = new byte[8]; // Largest data type is 64-bits (8 bytes)
    }

    @Override
    public int available() throws IOException {
        return dataInStream.available();
    }

    @Override
    public final int read(byte refBuffer[], int offset, int readLen) throws IOException {
        return inStream.read(refBuffer, offset, readLen);
    }

    @Override
    public int read() throws IOException {
        return inStream.read();
    }

    @Override
    public final int readUnsignedByte() throws IOException {
        return dataInStream.readUnsignedByte();
    }

    @Deprecated
    @Override
    public final String readLine() throws IOException {
        return dataInStream.readLine();
    }

    @Override
    public final String readUTF() throws IOException {
        return dataInStream.readUTF();
    }

    @Override
    public final void close() throws IOException {
        dataInStream.close();
    }

    @Override
    public final void readFully(byte refBuffer[]) throws IOException {
        dataInStream.readFully(refBuffer, 0, refBuffer.length);
    }

    @Override
    public final void readFully(byte refBuffer[], int offset, int readLen) throws IOException {
        dataInStream.readFully(refBuffer, offset, readLen);
    }

    @Override
    public final int skipBytes(int n) throws IOException {
        return dataInStream.skipBytes(n);
    }

    @Override
    public final boolean readBoolean() throws IOException {
        return dataInStream.readBoolean();
    }

    @Override
    public final byte readByte() throws IOException {
        return dataInStream.readByte();
    }

    @Override
    public final float readFloat() throws IOException {
        return Float.intBitsToFloat(readInt());
    }

    @Override
    public final double readDouble() throws IOException {
        return Double.longBitsToDouble(readLong());
    }

    @Override
    public final short readShort() throws IOException {
        return dataInStream.readShort();
    }

    @Override
    public final int readUnsignedShort() throws IOException {
        return dataInStream.readUnsignedShort();
    }

    @Override
    public final long readLong() throws IOException {
        return dataInStream.readLong();
    }

    @Override
    public final char readChar() throws IOException {
        return dataInStream.readChar();
    }

    @Override
    public final int readInt() throws IOException {
        return dataInStream.readInt();
    }

    /**
     * Reads floating point type stored in little endian (see readFloat() for big endian)
     * @return float value translated from little endian
     * @throws IOException if an IO error occurs
     */
    public final float readLittleFloat() throws IOException {
        return Float.intBitsToFloat(readLittleInt());
    }

    /**
     * Reads double precision floating point type stored in little endian (see readDouble() for big endian)
     * @return double precision float value translated from little endian
     * @throws IOException if an IO error occurs
     */    
    public final double readLittleDouble() throws IOException {
        return Double.longBitsToDouble(readLittleLong());
    }

    /**
     * Reads short type stored in little endian (see readShort() for big endian)
     * @return short value translated from little endian
     * @throws IOException if an IO error occurs
     */    
    public final short readLittleShort() throws IOException {
    dataInStream.readFully(byteBuffer, 0, 2);
    return (short)((byteBuffer[1] & 0xff) << 8 | (byteBuffer[0] & 0xff));
    }

    /**
     * Reads char (16-bits) type stored in little endian (see readChar() for big endian)
     * @return char value translated from little endian
     * @throws IOException if an IO error occurs
     */    
    public final char readLittleChar() throws IOException {
        dataInStream.readFully(byteBuffer, 0, 2);
        return (char)((byteBuffer[1] & 0xff) << 8 | (byteBuffer[0] & 0xff));
    }

    /**
     * Reads integer type stored in little endian (see readInt() for big endian)
     * @return integer value translated from little endian
     * @throws IOException if an IO error occurs
     */        
    public final int readLittleInt() throws IOException {
        dataInStream.readFully(byteBuffer, 0, 4);
        return (byteBuffer[3]) << 24 | (byteBuffer[2] & 0xff) << 16 |
            (byteBuffer[1] & 0xff) << 8 | (byteBuffer[0] & 0xff);
    }

    /**
     * Reads long type stored in little endian (see readLong() for big endian)
     * @return long value translated from little endian
     * @throws IOException if an IO error occurs
     */        
    public final long readLittleLong() throws IOException {
        dataInStream.readFully(byteBuffer, 0, 8);
        return (long)(byteBuffer[7]) << 56 | (long)(byteBuffer[6]&0xff) << 48 |
            (long)(byteBuffer[5] & 0xff) << 40 | (long)(byteBuffer[4] & 0xff) << 32 |
            (long)(byteBuffer[3] & 0xff) << 24 | (long)(byteBuffer[2] & 0xff) << 16 |
            (long)(byteBuffer[1] & 0xff) <<  8 | (long)(byteBuffer[0] & 0xff);
    }

    /**
     * Reads unsigned short type stored in little endian (see readUnsignedShort() for big endian)
     * @return integer value representing unsigned short value translated from little endian
     * @throws IOException if an IO error occurs
     */        
    public final int readLittleUnsignedShort() throws IOException {
        dataInStream.readFully(byteBuffer, 0, 2);
        return ((byteBuffer[1] & 0xff) << 8 | (byteBuffer[0] & 0xff));
    }
}

问题答案:

readBoolean()读取一个字节。readLine()读取单个字节并将每个字节转换为一个char

readUTF()读取修改后的UTF-8(其代码单位大小为一个八位字节)。UTF-8
没有字节序。

这些方法没有字节顺序问题。

关于设计,请考虑类型是否需要为an,InputStream以及是否ByteBuffer可能有用。如果您没有使用标记/重置之类的功能,并且Closeable可能不会公开新类型:

public class Bytes {
  public static DataInput littleEndian(final DataInput decorated) {
    class LittleInput implements DataInput {
      private ByteBuffer buffer = ByteBuffer.allocate(8);

      public int readInt() throws IOException {
        buffer.clear();
        buffer.order(ByteOrder.BIG_ENDIAN)
            .putInt(decorated.readInt())
            .flip();
        return buffer.order(ByteOrder.LITTLE_ENDIAN)
            .getInt();
      }

      //TODO: other methods    
    }

    return new LittleInput();
  }

}

我注意到流行的Guava库已经具有LittleEndianDataInputStream。



 类似资料:
  • 问题内容: 假设我有以下设置 我们知道返回类型B。 当我做的时候 投射输入然后尝试调用吗? 调用上铸造的结果类型? 我发现很难确定,而且总是在谨慎的情况下打上额外的括号(对于可读性来说,这不是一个坏主意,但现在我很好奇) 尽管我看不到这将如何改变行为,但具体参考了。 问题答案: 等价于,即问题中的#2。 要获得#1,您必须编写。 Java语言规范没有在易于理解的摘要中指定运算符优先级。 Sedge

  • 问题内容: 该文件不在我的控制之下。大多数字节序列是有效的UTF-8,而不是ISO-8859-1(或其他编码)。我想尽我所能提取尽可能多的信息。 该文件包含一些非法字节序列,应将其替换为替换字符。 这不是一件容易的事,它认为它需要有关UTF-8状态机的一些知识。 Oracle有一个我需要做的包装器: UTF8ValidationFilter javadoc 是否有类似的东西可用(商业或免费软件)?

  • 我的问题主要与命令有关。如您所见,在“1:”处,生成的字节码是。在本例中,为什么使用,而不是或?我们可以看到,稍后在“11:”中使用,而不使用。这有什么原因吗,为什么开始时使用变量2,后来使用变量1? 抱歉,如果上面的措辞不好,我现在才开始在大学里学习Java字节码。 此外,我还试图从我们必须学会使用的列表中找出哪些命令使用了一个以上的字节。这是列表,有人能验证下面命令中的“字节用法”(我不确定正

  • 问题内容: 我有一个以字符串形式传递的句子,我正在对单词“ and”进行替换,我想用“”替换它。而且它不是用空格替换“和”一词。以下是我的逻辑示例。而当我调试此逻辑时,逻辑确实落入了句子。 这里有我想念的东西吗? 问题答案: 而当我调试此逻辑时,逻辑确实落入了句子。 是的,然后你放弃返回值。 Java中的字符串是不可变的-当你调用时,它不会更改现有字符串的内容-它会返回经过修改的新字符串。所以你要

  • 问题内容: 假设我的格式如下: 我想将小数点替换为空白,使其看起来像这样: 我该怎么做呢?我以为可以解决问题,但是当我尝试这样时: 我收到了一个错误消息,因为它可能不是字符。那是有道理的,那么我还能怎么完成我想要的? 问题答案: 如果您只是将单引号换成双引号,那么这将起作用,因为空字符串是合法值,而不是“空字符”,并且有重载。请记住,这是的超类型。

  • 我正在创建一个这样的GUID 这输出 根据维基百科,guid中有四个部分,这解释了为什么字节顺序在四组中切换。然而,维基百科的文章还指出,所有部分都以大端格式存储。显然前三部分不是Big Endian。guid的GetBytes()方法按照与创建时完全相同的顺序返回字节。这种行为的解释是什么?