Commit 67e9e7ff authored by amadionix's avatar amadionix
Browse files

fix serialization by reversing bytes array because of big endian vs little endian.

parent 77cd40e6
<component name="libraryTable">
<library name="Maven: commons-lang:commons-lang:2.5">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.5/commons-lang-2.5.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.5/commons-lang-2.5-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.5/commons-lang-2.5-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
......@@ -18,5 +18,6 @@
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:3.5.0" level="project" />
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.3.4" level="project" />
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:1.3" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.5" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -26,6 +26,11 @@
<artifactId>guava</artifactId>
<version>30.0-jre</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
import com.google.common.primitives.Ints;
import org.apache.commons.lang.ArrayUtils;
import java.io.*;
import java.nio.ByteBuffer;
......@@ -12,40 +13,6 @@ public class Main {
TestClassToSerialize myObject = new TestClassToSerialize();
fillObject(myObject);
createBinaryFile(myObject);
// readFile();
}
private static void readFile() throws IOException {
FileInputStream fis = new FileInputStream(PATHNAME);
int i;
byte[] bytes;
// name_len
bytes = new byte[8];
i = fis.read(bytes);
long nameLen = bytesToLong(bytes);
System.out.println("read " + i + " bytes" +
" for variable name_len, the content is: " + nameLen);
// fname
bytes = new byte[(int) nameLen];
i = fis.read(bytes);
String fName = new String(bytes);
System.out.println("read " + i + " bytes" +
" for variable fname, the content is: " + fName);
// feature_names_count
bytes = new byte[8];
i = fis.read(bytes);
long featureNamesCount = bytesToLong(bytes);
System.out.println("read " + i + " bytes" +
" for variable feature_names_count" +
", the content is: " + featureNamesCount);
fis.close();
}
public static long bytesToLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(bytes);
buffer.flip();//need flip
return buffer.getLong();
}
private static void createBinaryFile(TestClassToSerialize myObject) throws IOException {
......@@ -55,36 +22,16 @@ public class Main {
}
private static void writeObjectToFile(FileOutputStream fos, TestClassToSerialize myObject) throws IOException {
fos.write(longToBytes(myObject.getNameLen()));
// fos.write(myObject.getFname().getBytes());
// fos.write(longToBytes(myObject.getFeatureNamesCount()));
fos.write(intToByteArray(myObject.getNameLen()));
fos.write(myObject.getFname().getBytes());
fos.write(intToByteArray(myObject.getFeatureNamesCount()));
fos.close();
}
public static byte[] intToByteArray(int data) {
return Ints.toByteArray(data);
}
// public static final byte[] intToByteArray(int value) {
// return new byte[] {
// (byte)(value >>> 24),
// (byte)(value >>> 16),
// (byte)(value >>> 8),
// (byte)value};
// }
// public static byte[] intToByteArray( int data ) {
// byte[] result = new byte[4];
// result[0] = (byte) ((data & 0xFF000000) >> 24);
// result[1] = (byte) ((data & 0x00FF0000) >> 16);
// result[2] = (byte) ((data & 0x0000FF00) >> 8);
// result[3] = (byte) ((data & 0x000000FF) >> 0);
// return result;
// }
public static byte[] longToBytes(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(x);
return Arrays.copyOfRange(buffer.array(), 4, 8);
byte[] bytes = Ints.toByteArray(data);
ArrayUtils.reverse(bytes);
return bytes;
}
private static void fillObject(TestClassToSerialize myObject) {
......
import java.io.Serializable;
public class TestClassToSerialize implements Serializable {
private long nameLen;
private int nameLen;
private String fname;
// FEATURE NAMES
private long featureNamesCount;
private int featureNamesCount;
private long vnlen;
private String vname;
public void setNameLen(long nameLen) {
public void setNameLen(int nameLen) {
this.nameLen = nameLen;
}
......@@ -17,7 +17,7 @@ public class TestClassToSerialize implements Serializable {
this.fname = fname;
}
public void setFeatureNamesCount(long featureNamesCount) {
public void setFeatureNamesCount(int featureNamesCount) {
this.featureNamesCount = featureNamesCount;
}
......@@ -29,7 +29,7 @@ public class TestClassToSerialize implements Serializable {
this.vname = vname;
}
public long getNameLen() {
public int getNameLen() {
return nameLen;
}
......@@ -37,7 +37,7 @@ public class TestClassToSerialize implements Serializable {
return fname;
}
public long getFeatureNamesCount() {
public int getFeatureNamesCount() {
return featureNamesCount;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment