Exemplo n.º 1
0
  private void runShortTest(Output write) throws IOException {
    write.writeShort(0);
    write.writeShort(63);
    write.writeShort(64);
    write.writeShort(127);
    write.writeShort(128);
    write.writeShort(8192);
    write.writeShort(16384);
    write.writeShort(32767);
    write.writeShort(-63);
    write.writeShort(-64);
    write.writeShort(-127);
    write.writeShort(-128);
    write.writeShort(-8192);
    write.writeShort(-16384);
    write.writeShort(-32768);

    Input read = new Input(write.toBytes());
    assertEquals(0, read.readShort());
    assertEquals(63, read.readShort());
    assertEquals(64, read.readShort());
    assertEquals(127, read.readShort());
    assertEquals(128, read.readShort());
    assertEquals(8192, read.readShort());
    assertEquals(16384, read.readShort());
    assertEquals(32767, read.readShort());
    assertEquals(-63, read.readShort());
    assertEquals(-64, read.readShort());
    assertEquals(-127, read.readShort());
    assertEquals(-128, read.readShort());
    assertEquals(-8192, read.readShort());
    assertEquals(-16384, read.readShort());
    assertEquals(-32768, read.readShort());
  }
 private static <T> T deserializeObjectByKryo(Kryo kryo, InputStream in, Class<T> clazz) {
   Input inp = new Input(in);
   kryo.setClassLoader(Utilities.getSessionSpecifiedClassLoader());
   T t = kryo.readObject(inp, clazz);
   inp.close();
   return t;
 }
Exemplo n.º 3
0
 @Override
 public void read(Kryo kryo, Input input) {
   playerId = input.readInt();
   targetId = input.readInt();
   time = input.readLong();
   meanOfDeath = kryo.readObject(input, EnumMeanOfDeath.class);
 }
Exemplo n.º 4
0
    @Override
    public void read(Kryo kryo, Input input) {
      objectID = input.readInt(true);

      int methodClassID = input.readInt(true);
      Class methodClass = kryo.getRegistration(methodClassID).getType();
      byte methodIndex = input.readByte();
      CachedMethod cachedMethod;
      try {
        cachedMethod = getMethods(kryo, methodClass)[methodIndex];
      } catch (IndexOutOfBoundsException ex) {
        throw new KryoException(
            "Invalid method index " + methodIndex + " for class: " + methodClass.getName());
      }
      method = cachedMethod.method;

      args = new Object[cachedMethod.serializers.length];
      for (int i = 0, n = args.length; i < n; i++) {
        Serializer serializer = cachedMethod.serializers[i];
        if (serializer != null) {
          args[i] = kryo.readObjectOrNull(input, method.getParameterTypes()[i], serializer);
        } else {
          args[i] = kryo.readClassAndObject(input);
        }
      }

      responseID = input.readByte();
    }
Exemplo n.º 5
0
  public void testStrings() throws IOException {
    runStringTest(new Output(4096));
    runStringTest(new Output(897));
    runStringTest(new Output(new ByteArrayOutputStream()));

    Output write = new Output(21);
    String value = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234";
    write.writeString(value);
    Input read = new Input(write.toBytes());
    assertEquals(value, read.readString());

    write.clear();
    write.writeString(null);
    read = new Input(write.toBytes());
    assertEquals(null, read.readString());

    for (int i = 0; i <= 258; i++) runStringTest(i);
    runStringTest(1);
    runStringTest(2);
    runStringTest(127);
    runStringTest(256);
    runStringTest(1024 * 1023);
    runStringTest(1024 * 1024);
    runStringTest(1024 * 1025);
    runStringTest(1024 * 1026);
    runStringTest(1024 * 1024 * 2);
  }
Exemplo n.º 6
0
 @Override
 public void read(Kryo kryo, Input in) {
   this.offset = BYTE_ARRAY_OFFSET;
   this.numBytes = in.readInt();
   this.base = new byte[numBytes];
   in.read((byte[]) base);
 }
Exemplo n.º 7
0
 public BigInteger read(Kryo kryo, Input input, Class<BigInteger> type) {
   int length = input.readVarInt(true);
   if (length == NULL) return null;
   byte[] bytes = input.readBytes(length - 1);
   if (type != BigInteger.class && type != null) {
     // For subclasses, use reflection
     try {
       Constructor<BigInteger> constructor = type.getConstructor(byte[].class);
       if (!constructor.isAccessible()) {
         try {
           constructor.setAccessible(true);
         } catch (SecurityException se) {
         }
       }
       return constructor.newInstance(bytes);
     } catch (Exception ex) {
       throw new KryoException(ex);
     }
   }
   if (length == 2) {
     // fast-path optimizations for BigInteger constants
     switch (bytes[0]) {
       case 0:
         return BigInteger.ZERO;
       case 1:
         return BigInteger.ONE;
       case 10:
         return BigInteger.TEN;
     }
   }
   return new BigInteger(bytes);
 }
Exemplo n.º 8
0
 @Override
 public <T> T decode(byte[] data, Class<T> classOfT) {
   Input input = null;
   input = new Input(data);
   T res = kryo.readObject(input, classOfT);
   input.close();
   return res;
 }
Exemplo n.º 9
0
 @Override
 public Object fromBinaryJava(byte[] bytes, Class<?> type) {
   Input input = new Input(bytes);
   try {
     return this.kryo.readObject(input, type);
   } finally {
     input.close();
   }
 }
Exemplo n.º 10
0
 @Override
 public void read(Kryo kryo, Input in) {
   this.baseOffset = BYTE_ARRAY_OFFSET;
   this.sizeInBytes = in.readInt();
   this.numFields = in.readInt();
   this.bitSetWidthInBytes = calculateBitSetWidthInBytes(numFields);
   this.baseObject = new byte[sizeInBytes];
   in.read((byte[]) baseObject);
 }
Exemplo n.º 11
0
 public byte[] readBinary() throws IOException {
   try {
     int length = input.readInt(true);
     byte[] result = new byte[length];
     input.readBytes(result);
     return result;
   } catch (KryoException e) {
     throw maybeEndOfStream(e);
   }
 }
 @Override
 public void read(Kryo kryo, Input input) {
   this.firstname = input.readString();
   this.lastname = input.readString();
   this.emailAddress = kryo.readObjectOrNull(input, SerializableEmailAddress.class);
   this.addresses = new HashSet<Address>();
   int numAddresses = input.readInt();
   for (int i = 0; i < numAddresses; i++) {
     this.add(kryo.readObject(input, SerializableAddress.class));
   }
 }
Exemplo n.º 13
0
 public Calendar read(Kryo kryo, Input input, Class<Calendar> type) {
   Calendar result = Calendar.getInstance(timeZoneSerializer.read(kryo, input, TimeZone.class));
   result.setTimeInMillis(input.readLong(true));
   result.setLenient(input.readBoolean());
   result.setFirstDayOfWeek(input.readInt(true));
   result.setMinimalDaysInFirstWeek(input.readInt(true));
   long gregorianChange = input.readLong(false);
   if (gregorianChange != DEFAULT_GREGORIAN_CUTOVER)
     if (result instanceof GregorianCalendar)
       ((GregorianCalendar) result).setGregorianChange(new Date(gregorianChange));
   return result;
 }
Exemplo n.º 14
0
  private void runBooleanTest(Output write) throws IOException {
    for (int i = 0; i < 100; i++) {
      write.writeBoolean(true);
      write.writeBoolean(false);
    }

    Input read = new Input(write.toBytes());
    for (int i = 0; i < 100; i++) {
      assertEquals(true, read.readBoolean());
      assertEquals(false, read.readBoolean());
    }
  }
Exemplo n.º 15
0
 public Pair<String, UrlPageModel> loadPageInfo(InputStream in) {
   synchronized (serializeLock) {
     Input input = null;
     try {
       input = new Input(in);
       String title = input.readString();
       UrlPageModel pageModel = kryo.readObject(input, UrlPageModel.class);
       return Pair.of(title, pageModel);
     } finally {
       IOUtils.closeQuietly(input);
     }
   }
 }
Exemplo n.º 16
0
 private static <T extends Serializable> T deserializeObjectFromKryo(
     byte[] bytes, Class<T> clazz) {
   Input inp = new Input(new ByteArrayInputStream(bytes));
   Kryo kryo = borrowKryo();
   T func = null;
   try {
     func = kryo.readObject(inp, clazz);
   } finally {
     releaseKryo(kryo);
   }
   inp.close();
   return func;
 }
Exemplo n.º 17
0
 public SerializablePage loadPage(InputStream in) {
   synchronized (serializeLock) {
     Input input = null;
     try {
       input = new Input(in);
       input.readString();
       kryo.readObject(input, UrlPageModel.class);
       return kryo.readObject(input, SerializablePage.class);
     } finally {
       IOUtils.closeQuietly(input);
     }
   }
 }
Exemplo n.º 18
0
 @Override
 public void read(Kryo kryo, Input input) {
   super.read(kryo, input);
   if (_has(VALUES)) {
     setValues(((List) readChildCollection(kryo, input)));
   }
   if (_has(NAMESPACE)) {
     setNameSpace(input.readString());
   }
   if (_has(IDENTITY)) {
     setIdentity(input.readString());
   }
 }
Exemplo n.º 19
0
 @Test
 public void testSerDeserPerf2() throws Exception {
   Kryo kryo = new Kryo();
   String outputPath = FilenameUtils.concat(getTmpPath(), "file2.bin");
   Output output = new Output(new FileOutputStream(outputPath));
   for (int i = 0; i < 1000; i++) {
     kryo.writeObject(output, constructNewPE());
   }
   output.close();
   Input input = new Input(new FileInputStream(outputPath));
   NewPartitionedEvent someObject = kryo.readObject(input, NewPartitionedEvent.class);
   input.close();
   Assert.assertTrue(someObject.getData().length == 1);
 }
Exemplo n.º 20
0
 @Override
 public HostLocation read(Kryo kryo, Input input, Class<HostLocation> type) {
   DeviceId deviceId = (DeviceId) kryo.readClassAndObject(input);
   PortNumber portNumber = (PortNumber) kryo.readClassAndObject(input);
   long time = input.readLong();
   return new HostLocation(deviceId, portNumber, time);
 }
Exemplo n.º 21
0
 public void readBytes(byte[] buffer, int offset, int count) throws EOFException {
   try {
     input.readBytes(buffer, offset, count);
   } catch (KryoException e) {
     throw maybeEndOfStream(e);
   }
 }
Exemplo n.º 22
0
 public URL read(Kryo kryo, Input input, Class<URL> type) {
   try {
     return new java.net.URL(input.readString());
   } catch (MalformedURLException e) {
     throw new KryoException(e);
   }
 }
Exemplo n.º 23
0
 public BigDecimal read(Kryo kryo, Input input, Class<BigDecimal> type) {
   BigInteger unscaledValue = bigIntegerSerializer.read(kryo, input, BigInteger.class);
   if (unscaledValue == null) return null;
   int scale = input.readInt(false);
   if (type != BigDecimal.class && type != null) {
     // For subclasses, use reflection
     try {
       Constructor<BigDecimal> constructor = type.getConstructor(BigInteger.class, int.class);
       if (!constructor.isAccessible()) {
         try {
           constructor.setAccessible(true);
         } catch (SecurityException se) {
         }
       }
       return constructor.newInstance(unscaledValue, scale);
     } catch (Exception ex) {
       throw new KryoException(ex);
     }
   }
   // fast-path optimizations for BigDecimal constants
   if (unscaledValue == BigInteger.ZERO && scale == 0) {
     return BigDecimal.ZERO;
   }
   // default behaviour
   return new BigDecimal(unscaledValue, scale);
 }
Exemplo n.º 24
0
 public Class read(Kryo kryo, Input input, Class<Class> type) {
   Registration registration = kryo.readClass(input);
   int isPrimitive = input.read();
   Class typ = registration != null ? registration.getType() : null;
   if (typ == null || !typ.isPrimitive()) return typ;
   return (isPrimitive == 1) ? typ : getWrapperClass(typ);
 }
Exemplo n.º 25
0
 public int readSmallInt() throws EOFException {
   try {
     return input.readInt(true);
   } catch (KryoException e) {
     throw maybeEndOfStream(e);
   }
 }
Exemplo n.º 26
0
  /** Read the edges from the temp file and load them to the graph. */
  private void readFromTempEdges(final Input input, final Graph graphToWriteTo) {
    while (!input.eof()) {
      // in this case the outId is the id assigned by the graph
      Object next = kryo.readClassAndObject(input);
      while (!next.equals(EdgeTerminator.INSTANCE)) {
        final List<Object> edgeArgs = new ArrayList<>();
        final DetachedEdge detachedEdge = (DetachedEdge) next;
        final Vertex vOut =
            graphToWriteTo.v(detachedEdge.iterators().vertexIterator(Direction.OUT).next().id());
        final Vertex inV =
            graphToWriteTo.v(detachedEdge.iterators().vertexIterator(Direction.IN).next().id());

        detachedEdge
            .iterators()
            .propertyIterator()
            .forEachRemaining(p -> edgeArgs.addAll(Arrays.asList(p.key(), p.value())));
        // detachedEdge.iterators().hiddenPropertyIterator().forEachRemaining(p ->
        // edgeArgs.addAll(Arrays.asList(Graph.Key.hide(p.key()), p.value())));

        appendToArgList(edgeArgs, T.id, detachedEdge.id());

        vOut.addEdge(detachedEdge.label(), inV, edgeArgs.toArray());

        next = kryo.readClassAndObject(input);
      }

      // vertex terminator
      kryo.readClassAndObject(input);
    }
  }
Exemplo n.º 27
0
 public String readNullableString() throws EOFException {
   try {
     return input.readString();
   } catch (KryoException e) {
     throw maybeEndOfStream(e);
   }
 }
Exemplo n.º 28
0
 public byte readByte() throws EOFException {
   try {
     return input.readByte();
   } catch (KryoException e) {
     throw maybeEndOfStream(e);
   }
 }
Exemplo n.º 29
0
 public long readSmallLong() throws EOFException, IOException {
   try {
     return input.readLong(true);
   } catch (KryoException e) {
     throw maybeEndOfStream(e);
   }
 }
  /**
   * @param sopremoRecord
   * @return
   */
  private SopremoRecord serializeAndDeserialize(SopremoRecord sopremoRecord) throws IOException {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeObject(output, sopremoRecord);
    output.close();
    baos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    Input input = new Input(bais);
    SopremoRecord deserialized = kryo.readObject(input, SopremoRecord.class);
    deserialized.setLayout(sopremoRecord.getLayout());
    input.close();

    return deserialized;
  }