You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
backend/src/main/java/com/xydl/cac/model/spectrogram/SpectrogramModel.java

58 lines
1.8 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.xydl.cac.model.spectrogram;
import lombok.Data;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
@Data
public class SpectrogramModel {
// 读取小端序的int4字节
public static int readLittleEndianInt(DataInputStream dis) throws IOException {
byte[] bytes = new byte[4];
dis.readFully(bytes);
return ByteBuffer.wrap(bytes)
.order(ByteOrder.LITTLE_ENDIAN)
.getInt();
}
// 读取小端序的short2字节
public static short readLittleEndianShort(DataInputStream dis) throws IOException {
byte[] bytes = new byte[2];
dis.readFully(bytes);
return ByteBuffer.wrap(bytes)
.order(ByteOrder.LITTLE_ENDIAN)
.getShort();
}
// 读取小端序的long8字节
public static long readLittleEndianLong(DataInputStream dis) throws IOException {
byte[] bytes = new byte[8];
dis.readFully(bytes);
return ByteBuffer.wrap(bytes)
.order(ByteOrder.LITTLE_ENDIAN)
.getLong();
}
// 读取小端序的float4字节
public static float readLittleEndianFloat(DataInputStream dis) throws IOException {
byte[] bytes = new byte[4];
dis.readFully(bytes);
return ByteBuffer.wrap(bytes)
.order(ByteOrder.LITTLE_ENDIAN)
.getFloat();
}
// 读取String
public static String readString(DataInputStream dis, int length) throws IOException {
byte[] bytes = new byte[length];
dis.readFully(bytes);
String str = new String(bytes, StandardCharsets.UTF_8);
return str.replaceAll("\u0000", "");
}
}