hprose.io.access.ShortFieldAccessor Maven / Gradle / Ivy
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* ShortFieldAccessor.java *
* *
* ShortFieldAccessor class for Java. *
* *
* LastModified: Aug 3, 2016 *
* Author: Ma Bingyao *
* *
\**********************************************************/
package hprose.io.access;
import hprose.common.HproseException;
import hprose.io.serialize.ValueWriter;
import hprose.io.serialize.Writer;
import hprose.io.unserialize.Reader;
import hprose.io.unserialize.ShortUnserializer;
import java.io.IOException;
import java.lang.reflect.Field;
public final class ShortFieldAccessor implements MemberAccessor {
private final long offset;
public ShortFieldAccessor(Field accessor) {
accessor.setAccessible(true);
offset = Accessors.unsafe.objectFieldOffset(accessor);
}
@Override
public void serialize(Writer writer, Object obj) throws IOException {
int value;
try {
value = Accessors.unsafe.getShort(obj, offset);
}
catch (Exception e) {
throw new HproseException(e.getMessage());
}
ValueWriter.write(writer.stream, value);
}
@Override
public void unserialize(Reader reader, Object obj) throws IOException {
short value = ShortUnserializer.instance.read(reader);
try {
Accessors.unsafe.putShort(obj, offset, value);
}
catch (Exception e) {
throw new HproseException(e.getMessage());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy