gpt4 book ai didi

Java : DataInputStream replacement for endianness

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:58:47 25 4
gpt4 key购买 nike

下面是我的代码,它替换了 DataInputStream 以包装 InputStream,但除了读取大端类型的常规方法之外,还提供了额外的方法来读取小端数据类型。如果您愿意,请随意使用它。

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

我的类层次结构在这里似乎有点奇怪。这样合适吗?

像 readUTF() 或 readLine() 这样的其他类型是否需要小端版本?或者这对特定程序来说是主观的?

Java 是如何存储 boolean 类型的?这也是字节顺序的主观因素吗?

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

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 有 no endianness .

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

关于设计,考虑类型是否需要是 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 .

关于Java : DataInputStream replacement for endianness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8028094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com