@Test
  public void encode_decode() throws Exception {
    when(wrappedTranscoder.encode(TEST_DATA))
        .thenReturn(new CachedData(0B0001, dataBytes, CachedData.MAX_SIZE));
    final CachedData encodedCachedData = snappyCompressWrapperTranscoder.encode(TEST_DATA);

    log.debug(
        "Original size : {}, snappy compressed size : {}",
        dataBytes.length,
        encodedCachedData.getData().length);
    assertThat(encodedCachedData.getFlags()).isEqualTo(0B1001);
    assertThat(encodedCachedData.getData().length).isLessThan(dataBytes.length);

    // decode
    when(wrappedTranscoder.decode(any(CachedData.class)))
        .thenAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                byte[] encodedBytes = ((CachedData) invocation.getArguments()[0]).getData();
                return new String(encodedBytes, "UTF-8");
              }
            });

    final Object decoded = snappyCompressWrapperTranscoder.decode(encodedCachedData);
    log.debug("Decoded : {}", decoded);
    assertThat(decoded).isEqualTo(TEST_DATA);
  }
 public void testCompressedObject() throws Exception {
   tc.setCompressionThreshold(8);
   Calendar c = Calendar.getInstance();
   CachedData cd = tc.encode(c);
   assertEquals(
       SerializingTranscoder.SERIALIZED | SerializingTranscoder.COMPRESSED, cd.getFlags());
   assertEquals(c, tc.decode(cd));
 }
 public void testCompressedString() throws Exception {
   // This one will actually compress
   String s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   tc.setCompressionThreshold(8);
   CachedData cd = tc.encode(s1);
   assertEquals(SerializingTranscoder.COMPRESSED, cd.getFlags());
   assertFalse(Arrays.equals(s1.getBytes(), cd.getData()));
   assertEquals(s1, tc.decode(cd));
 }
 public void testCompressedStringNotSmaller() throws Exception {
   String s1 = "This is a test simple string that will not be compressed.";
   // Reduce the compression threshold so it'll attempt to compress it.
   tc.setCompressionThreshold(8);
   CachedData cd = tc.encode(s1);
   // This should *not* be compressed because it is too small
   assertEquals(0, cd.getFlags());
   assertTrue(Arrays.equals(s1.getBytes(), cd.getData()));
   assertEquals(s1, tc.decode(cd));
 }
 public void testJsonObject() {
   String json =
       "{\"aaaaaaaaaaaaaaaaaaaaaaaaa\":" + "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}";
   tc.setCompressionThreshold(8);
   CachedData cd = tc.encode(json);
   assertFalse(
       "Flags shows JSON was compressed",
       (cd.getFlags() & (1L << SerializingTranscoder.COMPRESSED)) != 0);
   assertTrue("JSON was incorrectly encoded", Arrays.equals(json.getBytes(), cd.getData()));
   assertEquals("JSON was harmed, should not have been", json, tc.decode(cd));
 }
Ejemplo n.º 6
0
 @Override
 public Object decode(final CachedData d) {
   final byte[] bytes = d.getData();
   try (final Input input = new Input(new ByteArrayInputStream(bytes))) {
     final Object obj = kryo.readClassAndObject(input);
     return obj;
   }
 }
Ejemplo n.º 7
0
 /* (non-Javadoc)
  * @see net.spy.memcached.Transcoder#decode(net.spy.memcached.CachedData)
  */
 public Object decode(CachedData d) {
   byte[] data = d.getData();
   Object rv = null;
   if ((d.getFlags() & COMPRESSED) != 0) {
     data = decompress(d.getData());
   }
   int flags = d.getFlags() & SPECIAL_MASK;
   if ((d.getFlags() & SERIALIZED) != 0 && data != null) {
     rv = deserialize(data);
   } else if (flags != 0 && data != null) {
     switch (flags) {
       case SPECIAL_BOOLEAN:
         rv = Boolean.valueOf(tu.decodeBoolean(data));
         break;
       case SPECIAL_INT:
         rv = Integer.valueOf(tu.decodeInt(data));
         break;
       case SPECIAL_LONG:
         rv = Long.valueOf(tu.decodeLong(data));
         break;
       case SPECIAL_DATE:
         rv = new Date(tu.decodeLong(data));
         break;
       case SPECIAL_BYTE:
         rv = Byte.valueOf(tu.decodeByte(data));
         break;
       case SPECIAL_FLOAT:
         rv = new Float(Float.intBitsToFloat(tu.decodeInt(data)));
         break;
       case SPECIAL_DOUBLE:
         rv = new Double(Double.longBitsToDouble(tu.decodeLong(data)));
         break;
       case SPECIAL_BYTEARRAY:
         rv = data;
         break;
       default:
         getLogger().warn("Undecodeable with flags %x", flags);
     }
   } else {
     rv = decodeString(data);
   }
   return rv;
 }
 public void testObject() throws Exception {
   Calendar c = Calendar.getInstance();
   CachedData cd = tc.encode(c);
   assertEquals(SerializingTranscoder.SERIALIZED, cd.getFlags());
   assertEquals(c, tc.decode(cd));
 }
Ejemplo n.º 9
0
 @Override
 public I decode(final CachedData cachedData) {
   final Gson gson = new Gson();
   final String json = new String(cachedData.getData(), Charsets.UTF_8);
   return gson.fromJson(json, getObjectClass());
 }