protected Object unmarshall(byte[] source) throws IOException, ClassNotFoundException { return marshaller.objectFromByteBuffer(source); }
@Override public Object fromStorage(Object stored) { try { return stored instanceof byte[] ? marshaller.objectFromByteBuffer((byte[]) stored) : stored; } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } }
private Object unmarshall(Object key) { try { return marshaller.objectFromByteBuffer((byte[]) key); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return key; }
public Schema retrieveSchema(String name) throws IOException, InterruptedException, ClassNotFoundException { if (!knownSchemas.containsKey(name)) { byte[] key = marshaller.objectToByteBuffer(name); byte[] value = (byte[]) cacheManager.getCache(Support.AVRO_METADATA_CACHE_NAME).get(key); if (value==null) throw new IOException(name+" not found in the metadata cache"); Schema schema = (Schema) marshaller.objectFromByteBuffer(value); knownSchemas.put(name, schema); log.info("adding schama "+name+" to metadata cache"); } return knownSchemas.get(name); }
@Override public Object decodeValue(Object value) throws CodecException { if (value != null) { try { return marshaller.objectFromByteBuffer((byte[]) value); } catch (Exception e) { throw log.valueDecodingFailed(e, this.getName()); } } else { return null; } }
@Override public Object decodeKey(Object key) throws CodecException { if (key != null) { try { return marshaller.objectFromByteBuffer((byte[]) key); } catch (Exception e) { throw log.keyDecodingFailed(e, this.getName()); } } else { return null; } }
/** * Clones parameter x of type T with a given Marshaller reference; * * * @return a deep clone of an object parameter x */ @SuppressWarnings("unchecked") public static <T> T cloneWithMarshaller(Marshaller marshaller, T x){ if (marshaller == null) throw new IllegalArgumentException("Cannot use null Marshaller for clone"); byte[] byteBuffer; try { byteBuffer = marshaller.objectToByteBuffer(x); return (T) marshaller.objectFromByteBuffer(byteBuffer); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } catch (Exception e) { throw new CacheException(e); } }
@Override public Object decodeKey(Object key) throws CodecException { if (key != null) { try { return getMarshaller().objectFromByteBuffer((byte[]) key); } catch (Exception e) { throw log.keyDecodingFailed(e, this.getName()); } } else { return null; } }
@Override public Object decodeValue(Object value) throws CodecException { if (value != null) { try { return getMarshaller().objectFromByteBuffer((byte[]) value); } catch (Exception e) { throw log.valueDecodingFailed(e, this.getName()); } } else { return null; } } }
protected List<Object> executeQuery() { List<Object> results; QueryOperation op = ((OperationsFactory)cache.getOperationsFactory()).newAvroQueryOperation(this); Response response = op.execute(); results = new ArrayList<>(response.getResults().size()); for (ByteBuffer byteBuffer : response.getResults()) { try { results.add(cache.getRemoteCacheManager().getMarshaller().objectFromByteBuffer(byteBuffer.array())); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } numResults = response.getNumResults(); return results; }
@Test protected void testObjectMarshallingTest() throws Exception { List<ByteBuffer> serializedObjects = new ArrayList<>(testObjects.size()); for (TestObject object : testObjects) { serializedObjects.add(marshaller.objectToBuffer(object)); } assert serializedObjects.size() == testObjects.size(); for (int i = 0; i < testObjects.size(); i++) { byte[] bytes = serializedObjects.get(i).getBuf(); Object testObj = testObjects.get(i); Object unmarshalledObj = marshaller.objectFromByteBuffer(bytes); assert testObj.equals(unmarshalledObj); } }
@Test protected void testSerializingByteArrays() throws Exception { byte[] bytes = new byte[10]; IntStream.range(0, 10).forEach(i -> bytes[i] = (byte) i); byte[] marshalledBytes = marshaller.objectToBuffer(bytes).getBuf(); byte[] unmarsalledBytes = (byte[]) marshaller.objectFromByteBuffer(marshalledBytes); IntStream.range(0, 10).forEach(i -> assertEquals(unmarsalledBytes[i], i)); } }
@Test protected void testRegisterSerializersAreUtilised() throws Exception { TestObject obj = testObjects.get(0); byte[] bytes = marshaller.objectToByteBuffer(obj); assert marshaller.objectFromByteBuffer(bytes).equals(obj); checkCustomSerializerCounters(1, 1); }
@Test protected void testImmutableCollections() throws Exception { int listSize = 10; TestObject obj = testObjects.get(0); obj.setList(Collections.unmodifiableList(IntStream.range(0, 10).boxed().collect(Collectors.toList()))); byte[] bytes = marshaller.objectToByteBuffer(obj); TestObject unmarshalledObj = (TestObject) marshaller.objectFromByteBuffer(bytes); assert unmarshalledObj.getList().size() == listSize; for (int i = 0; i < listSize; i++) assert unmarshalledObj.getList().get(i) == i; }
public String fromStoredKey(RemoteCache cache, String key) throws IOException, InterruptedException, ClassNotFoundException { Object o = cache.getRemoteCacheManager().getMarshaller().objectFromByteBuffer(Base64.getDecoder().decode(key.substring(2))); log.tracef("Key in DB=%s > %s", key, o); return (String)o; }
/** * Converts an octet stream to a Java object * * @param source The source to convert * @param destination The type of the converted object. * @return an instance of a java object compatible with the supplied destination type. */ public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) { if (source == null) return null; if (!destination.match(MediaType.APPLICATION_OBJECT)) { throw log.invalidMediaType(APPLICATION_OBJECT_TYPE, destination.toString()); } String classType = destination.getClassType(); if (classType == null) return source; if (classType.equals("ByteArray")) { return source; } if (destination.hasStringType()) { return new String(source, UTF_8); } try { return marshaller.objectFromByteBuffer(source); } catch (IOException | ClassNotFoundException e) { throw log.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString()); } }
@Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { try { if (destinationType.equals(MediaType.APPLICATION_UNKNOWN) || contentType.equals(MediaType.APPLICATION_UNKNOWN)) { return content; } if (destinationType.match(marshaller.mediaType())) { return marshaller.objectToByteBuffer(content); } if (destinationType.match(MediaType.APPLICATION_OBJECT)) { return marshaller.objectFromByteBuffer((byte[]) content); } } catch (InterruptedException | IOException | ClassNotFoundException e) { throw new CacheException(e); } throw logger.unsupportedContent(content); }
+ handle.getFileId() + ":" + scheduledOffset); Object key = marshaller.objectFromByteBuffer(serializedKey);