Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Nicola Amadio
freud-java-serialization
Commits
67e9e7ff
Commit
67e9e7ff
authored
Dec 31, 2020
by
amadionix
Browse files
fix serialization by reversing bytes array because of big endian vs little endian.
parent
77cd40e6
Changes
6
Show whitespace changes
Inline
Side-by-side
.idea/libraries/Maven__commons_lang_commons_lang_2_5.xml
0 → 100644
View file @
67e9e7ff
<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
freud-java-serialization.iml
View file @
67e9e7ff
...
...
@@ -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
my-symbols/_nicola_test/idcm__nicola_test_12493.bin
View file @
67e9e7ff
No preview for this file type
pom.xml
View file @
67e9e7ff
...
...
@@ -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
src/main/java/Main.java
View file @
67e9e7ff
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
(
long
ToByte
s
(
myObject
.
getNameLen
()));
//
fos.write(myObject.getFname().getBytes());
//
fos.write(
long
ToByte
s
(myObject.getFeatureNamesCount()));
fos
.
write
(
int
ToByte
Array
(
myObject
.
getNameLen
()));
fos
.
write
(
myObject
.
getFname
().
getBytes
());
fos
.
write
(
int
ToByte
Array
(
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
)
{
...
...
src/main/java/TestClassToSerialize.java
View file @
67e9e7ff
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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment