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 @@ ...@@ -18,5 +18,6 @@
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:3.5.0" level="project" /> <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.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: com.google.j2objc:j2objc-annotations:1.3" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.5" level="project" />
</component> </component>
</module> </module>
\ No newline at end of file
...@@ -26,6 +26,11 @@ ...@@ -26,6 +26,11 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>30.0-jre</version> <version>30.0-jre</version>
</dependency> </dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
import org.apache.commons.lang.ArrayUtils;
import java.io.*; import java.io.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
...@@ -12,40 +13,6 @@ public class Main { ...@@ -12,40 +13,6 @@ public class Main {
TestClassToSerialize myObject = new TestClassToSerialize(); TestClassToSerialize myObject = new TestClassToSerialize();
fillObject(myObject); fillObject(myObject);
createBinaryFile(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 { private static void createBinaryFile(TestClassToSerialize myObject) throws IOException {
...@@ -55,36 +22,16 @@ public class Main { ...@@ -55,36 +22,16 @@ public class Main {
} }
private static void writeObjectToFile(FileOutputStream fos, TestClassToSerialize myObject) throws IOException { private static void writeObjectToFile(FileOutputStream fos, TestClassToSerialize myObject) throws IOException {
fos.write(longToBytes(myObject.getNameLen())); fos.write(intToByteArray(myObject.getNameLen()));
// fos.write(myObject.getFname().getBytes()); fos.write(myObject.getFname().getBytes());
// fos.write(longToBytes(myObject.getFeatureNamesCount())); fos.write(intToByteArray(myObject.getFeatureNamesCount()));
fos.close(); fos.close();
} }
public static byte[] intToByteArray(int data) { public static byte[] intToByteArray(int data) {
return Ints.toByteArray(data); byte[] bytes = Ints.toByteArray(data);
} ArrayUtils.reverse(bytes);
// public static final byte[] intToByteArray(int value) { return bytes;
// 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);
} }
private static void fillObject(TestClassToSerialize myObject) { private static void fillObject(TestClassToSerialize myObject) {
......
import java.io.Serializable; import java.io.Serializable;
public class TestClassToSerialize implements Serializable { public class TestClassToSerialize implements Serializable {
private long nameLen; private int nameLen;
private String fname; private String fname;
// FEATURE NAMES // FEATURE NAMES
private long featureNamesCount; private int featureNamesCount;
private long vnlen; private long vnlen;
private String vname; private String vname;
public void setNameLen(long nameLen) { public void setNameLen(int nameLen) {
this.nameLen = nameLen; this.nameLen = nameLen;
} }
...@@ -17,7 +17,7 @@ public class TestClassToSerialize implements Serializable { ...@@ -17,7 +17,7 @@ public class TestClassToSerialize implements Serializable {
this.fname = fname; this.fname = fname;
} }
public void setFeatureNamesCount(long featureNamesCount) { public void setFeatureNamesCount(int featureNamesCount) {
this.featureNamesCount = featureNamesCount; this.featureNamesCount = featureNamesCount;
} }
...@@ -29,7 +29,7 @@ public class TestClassToSerialize implements Serializable { ...@@ -29,7 +29,7 @@ public class TestClassToSerialize implements Serializable {
this.vname = vname; this.vname = vname;
} }
public long getNameLen() { public int getNameLen() {
return nameLen; return nameLen;
} }
...@@ -37,7 +37,7 @@ public class TestClassToSerialize implements Serializable { ...@@ -37,7 +37,7 @@ public class TestClassToSerialize implements Serializable {
return fname; return fname;
} }
public long getFeatureNamesCount() { public int getFeatureNamesCount() {
return featureNamesCount; return featureNamesCount;
} }
......
Markdown is supported
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