-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2019 Odnoklassniki Ltd, Mail.Ru Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package one.nio.serial; | ||
|
||
import one.nio.util.Hash; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* A wrapper for a preserialized object. Helps to avoid double serialization. | ||
* The wrapper automatically unpacks the original object during deserialization. | ||
*/ | ||
public class SerializedWrapper<T> implements Serializable { | ||
private final byte[] serialized; | ||
|
||
public SerializedWrapper(byte[] serialized) { | ||
this.serialized = serialized; | ||
} | ||
|
||
public byte[] getSerialized() { | ||
return serialized; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Hash.xxhash(serialized, 0, serialized.length); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof SerializedWrapper)) { | ||
return false; | ||
} | ||
|
||
return Arrays.equals(serialized, ((SerializedWrapper) obj).serialized); | ||
} | ||
|
||
public static <T> SerializedWrapper<T> wrap(T object) throws IOException { | ||
return new SerializedWrapper<>(Serializer.serialize(object)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public T unwrap() throws IOException, ClassNotFoundException { | ||
return (T) Serializer.deserialize(serialized); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2019 Odnoklassniki Ltd, Mail.Ru Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package one.nio.serial; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Asymmetric serializer that writes a preserialized object as byte[], | ||
* but reads an original object | ||
*/ | ||
class SerializedWrapperSerializer extends Serializer<Object> { | ||
|
||
SerializedWrapperSerializer() { | ||
super(SerializedWrapper.class); | ||
} | ||
|
||
@Override | ||
public void calcSize(Object obj, CalcSizeStream css) throws IOException { | ||
css.add(getSerialized(obj).length); | ||
} | ||
|
||
@Override | ||
public void write(Object obj, DataStream out) throws IOException { | ||
out.write(getSerialized(obj)); | ||
} | ||
|
||
@Override | ||
public Object read(DataStream in) throws IOException, ClassNotFoundException { | ||
Object result; | ||
try (Closeable blankScope = in.newScope()) { | ||
result = in.readObject(); | ||
} | ||
in.register(result); | ||
return result; | ||
} | ||
|
||
@Override | ||
public void skip(DataStream in) throws IOException, ClassNotFoundException { | ||
try (Closeable blankScope = in.newScope()) { | ||
in.readObject(); | ||
} | ||
} | ||
|
||
@Override | ||
public void toJson(Object obj, StringBuilder builder) throws IOException { | ||
Json.appendBinary(builder, getSerialized(obj)); | ||
} | ||
|
||
@Override | ||
public Object fromJson(JsonReader in) throws IOException, ClassNotFoundException { | ||
return Serializer.deserialize(in.readBinary()); | ||
} | ||
|
||
private static byte[] getSerialized(Object obj) { | ||
return ((SerializedWrapper) obj).getSerialized(); | ||
} | ||
} |
This file contains 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