Commit 2f60b265 authored by amadionix's avatar amadionix
Browse files

refactor, clean-up, add java bytes reader: it works in Java, but not yet with reader.cc

parent d88d81bd
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="freud-java-serialization" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="freud-java-serialization" target="11" />
</bytecodeTargetLevel>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="11" project-jdk-type="JavaSDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/freud-java-serialization.iml" filepath="$PROJECT_DIR$/freud-java-serialization.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
......@@ -11,6 +11,5 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
</component>
</module>
\ No newline at end of file
No preview for this file type
......@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>freud-experiments</artifactId>
<artifactId>freud-java-serialization</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
......@@ -13,20 +13,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
import com.google.common.primitives.Longs;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
TestClassToSerialize myObject = new TestClassToSerialize();
public static final String PATHNAME = "my-symbols/idcm__fle";
public static void main(String[] args) throws IOException {
TestClassToSerialize myObject = new TestClassToSerialize();
fillObject(myObject);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("my-symbols/idcm__fle");
writeObjectToFile(fos, myObject);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
createBinaryFile(myObject);
readFile();
}
private static void writeObjectToFile(FileOutputStream fos, TestClassToSerialize myObject) {
try {
fos.write(anotherOne(myObject.getNameLen()));
fos.write(myObject.getFname().getBytes());
fos.write(anotherOne(myObject.getFeatureNamesCount()));
// fos.write(fromUnsignedInt16(myObject.getVnlen()));
// fos.write(myObject.getVname().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
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 byte[] anotherOne(long val) {
public static long bytesToLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(0, val);
return Arrays.copyOfRange(buffer.array(), 4, 8);
buffer.put(bytes);
buffer.flip();//need flip
return buffer.getLong();
}
private static void createBinaryFile(TestClassToSerialize myObject) throws IOException {
File myFile = new File(PATHNAME);
myFile.createNewFile();
writeObjectToFile(new FileOutputStream(myFile), myObject);
}
// return Arrays.copyOfRange(Longs.toByteArray(val), 4, 8);
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.close();
}
// public static byte[] fromUnsignedInt(long value) {
// byte[] bytes = new byte[8];
// ByteBuffer.wrap(bytes).putLong(value);
// return Arrays.copyOfRange(bytes, 4, 8);
// }
public static byte[] longToBytes(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(x);
return buffer.array();
}
private static void fillObject(TestClassToSerialize myObject) {
myObject.setNameLen(9);
myObject.setFname("idcm__fle");
myObject.setFeatureNamesCount(1);
myObject.setVname("as");
myObject.setVnlen(2);
// myObject.setVnlen(2);
// myObject.setVname("as");
}
// public static byte[] fromUnsignedInt16(long value) {
// byte[] bytes = new byte[8];
// ByteBuffer.wrap(bytes).putLong(value);
// return Arrays.copyOfRange(bytes, 6, 8);
// }
}
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