下面列出了org.apache.hadoop.io.ArrayPrimitiveWritable#com.esotericsoftware.kryo.io.Input 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public DefaultLink read(Kryo kryo, Input input, Class<DefaultLink> type) {
ProviderId providerId = (ProviderId) kryo.readClassAndObject(input);
ConnectPoint src = (ConnectPoint) kryo.readClassAndObject(input);
ConnectPoint dst = (ConnectPoint) kryo.readClassAndObject(input);
Type linkType = (Type) kryo.readClassAndObject(input);
State state = (State) kryo.readClassAndObject(input);
boolean isDurable = input.readBoolean();
return DefaultLink.builder()
.providerId(providerId)
.src(src)
.dst(dst)
.type(linkType)
.state(state)
.isExpected(isDurable)
.build();
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public T read(Kryo kryo, Input input, Class aClass) {
try {
ObjectMap graphContext = kryo.getGraphContext();
ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this);
if (objectStream == null) {
// make sure we use Kryo's classloader
objectStream = new InstantiationUtil.ClassLoaderObjectInputStream(input, kryo.getClassLoader());
graphContext.put(this, objectStream);
}
return (T) objectStream.readObject();
} catch (Exception ex) {
throw new KryoException("Error during Java deserialization.", ex);
}
}
@Test
public void testDataFileKryoSerialization() throws Exception {
File data = temp.newFile();
Assert.assertTrue(data.delete());
Kryo kryo = new KryoSerializer(new SparkConf()).newKryo();
try (Output out = new Output(new FileOutputStream(data))) {
kryo.writeClassAndObject(out, DATA_FILE);
kryo.writeClassAndObject(out, DATA_FILE.copy());
}
try (Input in = new Input(new FileInputStream(data))) {
for (int i = 0; i < 2; i += 1) {
Object obj = kryo.readClassAndObject(in);
Assert.assertTrue("Should be a DataFile", obj instanceof DataFile);
checkDataFile(DATA_FILE, (DataFile) obj);
}
}
}
public ArrayMetaData<byte[]> read(Kryo kryo, Input input, Class<ArrayMetaData<byte[]>> type) {
try {
byte[] arrData = arrayMetaData.getArrData();
thatArrMetaData = arrayMetaData.recv(input);
int arrSegNum = thatArrMetaData.getSegNum();
for (int i = 0; i < arrSegNum; i++) {
int from = thatArrMetaData.getFrom(i);
int to = thatArrMetaData.getTo(i);
for (int j = from; j < to; j++) {
arrData[j] = input.readByte();
}
}
thatArrMetaData.setArrData(arrData);
} catch (IOException e) {
LOG.error("double array read exception", e);
System.exit(1);
}
return thatArrMetaData;
}
@Test
public void testEmptyByteSlice() {
ByteSlice byteSliceIn = ByteSlice.wrap(new byte[] {0, 1, 2,3,4,5,6,7,8,9}, 5, 0);
Output output = new Output(new byte[20],20);
kryo.writeObject(output, byteSliceIn);
byte[] bytes = output.toBytes();
assertNotNull(bytes);
Input input = new Input(new ByteArrayInputStream(bytes), bytes.length);
ByteSlice byteSliceOut = kryo.readObject(input, ByteSlice.class);
assertNotNull(byteSliceOut);
assertEquals(0, byteSliceOut.offset());
assertEquals(0, byteSliceOut.length());
}
@SuppressWarnings("unchecked")
@Test
public void testHopscotchSetFromFasta() throws Exception {
final String libraryPath = publicTestDir + PathSeqBuildKmers.class.getPackage().getName().replace(".", "/") + "/hg19mini.hss";
final File expectedFile = new File(libraryPath);
final File ref = new File(hg19MiniReference);
final File output = createTempFile("test", ".hss");
if (!output.delete()) {
Assert.fail();
}
final ArgumentsBuilder args = new ArgumentsBuilder();
args.add(PathSeqBuildKmers.REFERENCE_LONG_NAME, ref);
args.addOutput(output);
this.runCommandLine(args.getArgsArray());
final Input inputExpected = new Input(FileUtils.openInputStream(expectedFile));
final Input inputTest = new Input(FileUtils.openInputStream(output));
final Kryo kryo = new Kryo();
final PSKmerSet expectedKmerLib = kryo.readObject(inputExpected, PSKmerSet.class);
final PSKmerSet testKmerLib = kryo.readObject(inputTest, PSKmerSet.class);
Assert.assertEquals(testKmerLib, expectedKmerLib);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
Kryo kryo = kryos.get();
kryo.setReferences(false);
kryo.register(clazz);
try (Input input = new Input(bytes)) {
return (T) kryo.readClassAndObject(input);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
public ArrayMetaData<double[]> read(Kryo kryo, Input input, Class<ArrayMetaData<double[]>> type) {
try {
double[] arrData = arrayMetaData.getArrData();
thatArrMetaData = arrayMetaData.recv(input);
int arrSegNum = thatArrMetaData.getSegNum();
for (int i = 0; i < arrSegNum; i++) {
int from = thatArrMetaData.getFrom(i);
int to = thatArrMetaData.getTo(i);
for (int j = from; j < to; j++) {
arrData[j] = input.readDouble();
}
}
thatArrMetaData.setArrData(arrData);
} catch (IOException e) {
LOG.error("double array read exception", e);
System.exit(1);
}
return thatArrMetaData;
}
@SuppressWarnings("unchecked")
@Test
public void testMaskedHopscotchSetFromFasta() throws Exception {
final File expectedFile = getTestFile("hg19mini.mask_4_15.hss");
final File ref = new File(hg19MiniReference);
final File output = createTempFile("test", ".hss");
if (!output.delete()) {
Assert.fail();
}
final ArgumentsBuilder args = new ArgumentsBuilder();
args.add(PathSeqBuildKmers.REFERENCE_LONG_NAME, ref);
args.add(PathSeqBuildKmers.KMER_MASK_LONG_NAME, "4,15");
args.addOutput(output);
this.runCommandLine(args.getArgsArray());
final Input inputExpected = new Input(FileUtils.openInputStream(expectedFile));
final Input inputTest = new Input(FileUtils.openInputStream(output));
final Kryo kryo = new Kryo();
final PSKmerSet expectedKmerLib = kryo.readObject(inputExpected, PSKmerSet.class);
final PSKmerSet testKmerLib = kryo.readObject(inputTest, PSKmerSet.class);
Assert.assertEquals(testKmerLib, expectedKmerLib);
}
@Test
void serializationTest() {
final Random rng = new Random(RAND_SEED);
final LongBloomFilter bloomFilter = new LongBloomFilter(HHASH_NVALS, FPP);
final HashSet<Long> hashSet = new HashSet<>(HHASH_NVALS);
for (int valNo = 0; valNo != HHASH_NVALS; ++valNo) {
final long randLong = randomLong(rng);
bloomFilter.add(randLong);
hashSet.add(randLong);
}
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final Output out = new Output(bos);
final Kryo kryo = new Kryo();
kryo.writeObject(out, bloomFilter);
out.flush();
final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
final Input in = new Input(bis);
final LongBloomFilter bloomFilter2 = kryo.readObject(in, LongBloomFilter.class);
Assert.assertEquals(bloomFilter, bloomFilter2);
for (Long val : hashSet) {
Assert.assertTrue(bloomFilter2.contains(val));
}
}
/**
* Loads the project context .pro.
*
* Does however not load the scenes (only the scene names as reference) or
* meshes/textures (see ProjectManager).
*
* @param ref
* project to load
* @return loaded project context without scenes
* @throws FileNotFoundException
*/
public ProjectContext loadProjectContext(ProjectRef ref) throws FileNotFoundException {
// find .pro file
FileHandle projectFile = null;
for (FileHandle f : Gdx.files.absolute(ref.getPath()).list()) {
if (f.extension().equals(ProjectManager.PROJECT_EXTENSION)) {
projectFile = f;
break;
}
}
if (projectFile != null) {
Input input = new Input(new FileInputStream(projectFile.path()));
ProjectDescriptor projectDescriptor = kryo.readObjectOrNull(input, ProjectDescriptor.class);
ProjectContext context = DescriptorConverter.convert(projectDescriptor);
context.activeSceneName = projectDescriptor.getCurrentSceneName();
return context;
}
return null;
}
@Test
public void testEncodingDecodingSeveralRows() throws IOException, StandardException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Output output = new Output(out);
for (int i = 0; i < 100; i++) {
kryo.writeClassAndObject(output, getExecRow(i, 13));
}
output.close();
InputStream in = new ByteArrayInputStream(out.toByteArray());
Input input = new Input(in);
for (int i = 0; i < 100; i++) {
ExecRow row = (ExecRow) kryo.readClassAndObject(input);
assertEquals(i, row.getRowArray()[0].getInt());
assertEquals(""+i, row.getRowArray()[1].getString());
}
input.close();
}
@Test
public void testByteSlice() {
ByteSlice byteSliceIn = ByteSlice.wrap(new byte[] {0, 1, 2,3,4,5,6,7,8,9}, 2, 4);
Output output = new Output(new byte[20],20);
kryo.writeObject(output, byteSliceIn);
byte[] bytes = output.toBytes();
assertNotNull(bytes);
Input input = new Input(new ByteArrayInputStream(bytes), bytes.length);
ByteSlice byteSliceOut = kryo.readObject(input, ByteSlice.class);
assertNotNull(byteSliceOut);
assertEquals(0, byteSliceOut.offset());
assertEquals(4, byteSliceOut.length());
}
public static void main(String[] args) {
Kryo kryo = new Kryo();
// 序列化
ByteArrayOutputStream os = new ByteArrayOutputStream();
Output output = new Output(os);
TestUser user = new TestUser();
kryo.writeObject(output, user);
output.close();
byte[] bytes = os.toByteArray();
// 反序列化
Input input = new Input(new ByteArrayInputStream(bytes));
user = kryo.readObject(input, TestUser.class);
input.close();
}
/**
* Read an instance of the given class from the given input.
*
* @param kryo instance of {@link Kryo} object
* @param input a Kryo {@link Input}
* @param aClass The class of the object to be read in.
* @return an instance of type <T>
*/
@Override
public T read(Kryo kryo, Input input, Class<T> aClass) {
// read the serialized data
byte[] data = new byte[input.readInt()];
input.readBytes(data);
try {
try(ByteArrayInputStream stream = new ByteArrayInputStream(data)) {
HTMObjectInput reader = serializer.getObjectInput(stream);
T t = (T) reader.readObject(aClass);
postDeSerialize(t);
return t;
}
}
catch(Exception e) {
throw new KryoException(e);
}
}
public ArrayMetaData<String[]> read(Kryo kryo, Input input, Class<ArrayMetaData<String[]>> type) {
try {
String[] arrData = arrayMetaData.getArrData();
thatArrMetaData = arrayMetaData.recv(input);
int arrSegNum = thatArrMetaData.getSegNum();
for (int i = 0; i < arrSegNum; i++) {
int from = thatArrMetaData.getFrom(i);
int to = thatArrMetaData.getTo(i);
for (int j = from; j < to; j++) {
arrData[j] = input.readString();
}
}
thatArrMetaData.setArrData(arrData);
} catch (IOException e) {
LOG.error("double array read exception", e);
System.exit(1);
}
return thatArrMetaData;
}
@SuppressWarnings("unchecked")
@Override
public void readFields(final DataInput input) throws IOException {
try {
Kryo kryo = new Kryo();
kryo.register(HBaseEdge.class, new HBaseEdgeSerializer());
final ByteArrayInputStream bais = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input));
this.edge = kryo.readObject(new Input(bais), HBaseEdge.class);
Class<? extends Writable> cls = Class.forName(Text.readString(input)).asSubclass(Writable.class);
Writable writable;
if (cls.equals(NullWritable.class)) {
writable = NullWritable.get();
} else {
writable = cls.newInstance();
}
writable.readFields(input);
this.value = writable != NullWritable.get() ? (V) writable : null;
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new IOException("Failed writable init", e);
}
}
@Override
public T apply(byte[] value) {
if (value == null || value.length == 0) {
return null;
}
T v = null;
final ByteArrayInputStream inputStream = new ByteArrayInputStream(value);
try {
Input input = new Input(inputStream);
v = kryo.readObject(input, type);
input.close();
} catch (Exception e) {
Exceptions.handle(e);
}
return v;
}
@Override
public IpAddress read(Kryo kryo, Input input, Class<IpAddress> type) {
final int octLen = input.readInt();
checkArgument(octLen <= IpAddress.INET6_BYTE_LENGTH);
byte[] octs = new byte[octLen];
input.readBytes(octs);
// Use the address size to decide whether it is IPv4 or IPv6 address
if (octLen == IpAddress.INET_BYTE_LENGTH) {
return IpAddress.valueOf(IpAddress.Version.INET, octs);
}
if (octLen == IpAddress.INET6_BYTE_LENGTH) {
return IpAddress.valueOf(IpAddress.Version.INET6, octs);
}
return null; // Shouldn't be reached
}
@Override
public void read(final Object object, final Input in) throws KryoException
{
final StreamingContainerManager scm = (StreamingContainerManager)object;
final int operatorId = in.readInt();
final String propertyName = in.readString();
final String propertyValue = in.readString();
final PTOperator o = scm.plan.getAllOperators().get(operatorId);
if (o == null) {
throw new IllegalArgumentException("Unknown physical operator " + operatorId);
}
scm.setPhysicalOperatorProperty(o, propertyName, propertyValue);
}
/**
* Clone from the binary data of the source object
* @return T
*/
public T getClone()
{
try (Input input = new Input(bin)) {
return kryo.readObject(input, clazz);
}
}
public KryoTupleDeserializer(final Map conf, final GeneralTopologyContext context, final StormTopology topology) {
_kryo = new KryoValuesDeserializer(conf);
_context = context;
_ids = new SerializationFactory.IdDictionary(topology);
_kryoInput = new Input(1);
_ackerNum = JStormUtils.parseInt(conf.get(Config.TOPOLOGY_ACKER_EXECUTORS), 0);
_isTransactionTuple = JStormUtils.parseBoolean(conf.get(ConfigExtension.TRANSACTION_TOPOLOGY), false);
}
@Override
public void read(final Object object, final Input in) throws KryoException
{
PhysicalPlan plan = (PhysicalPlan)object;
int containerId = in.readInt();
for (PTContainer c : plan.getContainers()) {
if (c.getId() == containerId) {
int stateOrd = in.readInt();
c.state = PTContainer.State.values()[stateOrd];
c.containerId = in.readString();
c.resourceRequestPriority = in.readInt();
c.requiredMemoryMB = in.readInt();
c.allocatedMemoryMB = in.readInt();
c.requiredVCores = in.readInt();
c.allocatedVCores = in.readInt();
String bufferServerHost = in.readString();
if (bufferServerHost != null) {
c.bufferServerAddress = InetSocketAddress.createUnresolved(bufferServerHost, in.readInt());
}
c.host = in.readString();
c.nodeHttpAddress = in.readString();
int tokenLength = in.readInt();
if (tokenLength != -1) {
c.bufferServerToken = in.readBytes(tokenLength);
} else {
c.bufferServerToken = null;
}
break;
}
}
}
public static Input getInput(InputBuf inputBuf) {
ByteBuffer nioBuf = inputBuf.nioByteBuffer();
ByteBufferInput input = new ByteBufferInput();
input.setVarIntsEnabled(false); // Compatible with FastInput
input.setBuffer(nioBuf, 0, nioBuf.capacity());
return input;
}
public static PSKmerCollection readKmerFilter(final String uri) {
final Input input = new Input(BucketUtils.openFile(uri));
final Kryo kryo = new Kryo();
if (uri.endsWith(HOPSCOTCH_SET_EXTENSION)) {
return kryo.readObject(input, PSKmerSet.class);
} else if (uri.endsWith(BLOOM_FILTER_EXTENSION)) {
return kryo.readObject(input, PSKmerBloomFilter.class);
}
throw new UserException.BadInput("Unknown kmer set extension in file name " + uri);
}
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override public void read(Kryo kryo, Input in) {
intVal = kryo.readObject(in, Integer.class);
longVal = kryo.readObject(in, Long.class);
boolVal = kryo.readObject(in, Boolean.class);
longArr = kryo.readObject(in, long[].class);
dblArr = kryo.readObject(in, double[].class);
if (MARSHAL_COLS_AS_OBJECTS) {
list = kryo.readObject(in, ArrayList.class);
map = kryo.readObject(in, HashMap.class);
}
else {
int listSize = kryo.readObject(in, Integer.class);
list = new ArrayList<>(listSize);
for (int i = 0; i < listSize; i++)
list.add(kryo.readObject(in, Float.class));
int mapSize = kryo.readObject(in, Integer.class);
map = new HashMap<>(mapSize);
for (int i = 0; i < mapSize; i++)
map.put(kryo.readObject(in, Integer.class), kryo.readObject(in, Character.class));
}
selfRef = kryo.readObject(in, TestObject.class);
}
@Override
public Object deserialize(ByteBuffer rawMessage) {
Input input = new Input(rawMessage.array());
try {
return kryoThreadLocal.get().readClassAndObject(input);
} finally {
input.close();
}
}
@Override
public InternalDeviceStatusChangeEvent read(Kryo kryo, Input input,
Class<InternalDeviceStatusChangeEvent> type) {
DeviceId deviceId = kryo.readObject(input, DeviceId.class, deviceIdSerializer());
Timestamp timestamp = (Timestamp) kryo.readClassAndObject(input);
Boolean available = (Boolean) kryo.readClassAndObject(input);
return new InternalDeviceStatusChangeEvent(deviceId, timestamp, available);
}
@Test
public void serDeTest() throws IOException {
final Kryo kryo = new Kryo();
File destination = File.createTempFile("partition_", ".gz");
final Output output = new Output(new GZIPOutputStream(new FileOutputStream(destination)));
int numColumns = 6;
int numValues = 6;
OffHeapPartition p = createPartition();
List<DataRow> data = TestUtils.genMultiColumnData("f1", numColumns, numValues);
data.addAll(TestUtils.genMultiColumnData("f2", numColumns, numValues));
p.insertData(data);
log.info("Writing partition with {} records to {}", data.size(), destination.getAbsolutePath());
long t0 = System.nanoTime();
kryo.writeObject(output, p);
output.close();
long t1 = System.nanoTime();
log.info("Took {} ms to write {} records", (t1 - t0) / 1000000, data.size());
// Now reading the file
Input input = new Input(new GZIPInputStream(new FileInputStream(destination)));
t0 = System.nanoTime();
OffHeapPartition newP = kryo.readObject(input, OffHeapPartition.class);
t1 = System.nanoTime();
assertEquals(p.getNumRecords(), newP.getNumRecords());
long cP =
TestUtils.checkMatchMultiFilter(
p, "c", "f1_0", "f1_0_0", "f1_0", "f1_0_1", "f1_0", "f1_0_2"); // ,
long cNp =
TestUtils.checkMatchMultiFilter(
newP, "c", "f1_0", "f1_0_0", "f1_0", "f1_0_1", "f1_0", "f1_0_2"); // ,
assertEquals(cP, cNp);
log.info("Took {} ms to read {} records", (t1 - t0) / 1000000, data.size());
log.info("{}", newP.getStats());
destination.deleteOnExit();
}
@Override
public ObjectName read(final Kryo kryo, final Input input, final Class<ObjectName> clazz) {
final String name = kryo.readObject(input, String.class);
try {
return ObjectNameCache.getObjectName(name);
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}