Last active
November 2, 2015 14:43
-
-
Save vadymhimself/0bdad8e0cf11008fc347 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.istack.internal.Nullable; | |
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.lang.reflect.Field; | |
import java.util.Arrays; | |
/** | |
* serializes only fields marked with @Save annotation | |
*/ | |
public interface MySerializable { | |
Class<?>[] PRIMITIVE_CLASSES = {boolean.class, byte.class, char.class, short.class, | |
int.class, long.class, float.class, double.class}; | |
enum Primitive {BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE} | |
default | |
@Nullable | |
byte[] getBytes() { | |
try (ByteOutputStream bos = new ByteOutputStream(); | |
DataOutputStream dos = new DataOutputStream(bos)) { | |
for (Field field : this.getClass().getDeclaredFields()) { | |
if (field.isAnnotationPresent(Save.class)) { | |
if (field.getType().isPrimitive()) { | |
try { | |
// set field accessible | |
field.setAccessible(true); | |
int type = Arrays.asList(PRIMITIVE_CLASSES).indexOf(field.getType()); | |
if (type >= 0 && type < PRIMITIVE_CLASSES.length) { | |
// first written byte is type | |
dos.writeByte(type); | |
// following bytes are field's value | |
Primitive enumType = Primitive.values()[type]; | |
switch (enumType) { | |
case CHAR: | |
dos.writeChar(field.getChar(this)); | |
break; | |
case INT: | |
dos.writeInt(field.getInt(this)); | |
break; | |
// TODO complete switch for all primitive types | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else { | |
throw new UnsupportedOperationException("Only primitive types is supported in MySerialization :D"); | |
} | |
} | |
} | |
dos.flush(); | |
dos.close(); | |
return bos.getBytes(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.istack.internal.Nullable; | |
import java.io.*; | |
import java.lang.reflect.Field; | |
public class MySerializer { | |
private MySerializer() {} | |
public static void saveToFile(String filePath, MySerializable mySerializable) throws IOException { | |
FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath)); | |
fileOutputStream.write(mySerializable.getBytes()); | |
fileOutputStream.flush(); | |
fileOutputStream.close(); | |
} | |
public | |
@Nullable | |
static <T> T restoreFromFile(String filepath, Class<T> cls) throws IOException { | |
try (FileInputStream fis = new FileInputStream(filepath); | |
DataInputStream dataInputStream = new DataInputStream(fis)) { | |
T object = cls.newInstance(); | |
for (Field field : cls.getDeclaredFields()) { | |
if (field.isAnnotationPresent(Save.class)) { | |
byte type = dataInputStream.readByte(); | |
// TODO if type != -1 | |
MySerializable.Primitive primitive = MySerializable.Primitive.values()[type]; | |
Object value; | |
switch (primitive) { | |
case INT: | |
value = dataInputStream.readInt(); | |
break; | |
case CHAR: | |
value = dataInputStream.readChar(); | |
break; | |
// TODO complete switch for all primitive types | |
default: | |
value = null; | |
} | |
field.set(object, value); | |
} | |
} | |
return object; | |
} catch (IOException e) { | |
throw e; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(value = ElementType.FIELD) | |
@Retention(value = RetentionPolicy.RUNTIME) | |
public @interface Save {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
/** | |
* Created by Vadim Ovcharenko on 02.11.2015. | |
*/ | |
public class Test implements MySerializable { | |
@Save | |
public int testInt = 1337; | |
@Save | |
public char testChar = 'f'; | |
public void test() { | |
System.out.println("testInt: " + testInt); | |
System.out.println("testChar: " + testChar); | |
} | |
public static void main(String... args) { | |
Test before = new Test(); | |
before.test(); | |
try { | |
MySerializer.saveToFile("C:\\test", before); | |
Test after = MySerializer.restoreFromFile("C:\\test", Test.class); | |
if (after != null) after.test(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
testInt: 1337
testChar: f
testInt: 1337
testChar: f