@Override public Vector next() { try { // refill the ByteBuffer if not enough bytes left to read if (bb.remaining() < Ints.BYTES) if (refill() < Ints.BYTES) throw new IllegalStateException("Could not read from input file"); // peek the size of the vector bb.mark(); // vector size in bytes = int + long + size * (int + double) final int vectorBytes = (bb.getInt() + 1) * (Ints.BYTES + Doubles.BYTES); bb.reset(); if (bb.remaining() < vectorBytes) if (refill() < vectorBytes) throw new IllegalStateException("Internal vector buffer too small"); // now we are sure that the ByteBuffer contains the whole next vector current.read(bb); } catch (IOException e) { e.printStackTrace(); throw new SerializationException(); } numReads++; return current; }
private static void saveResult(final AnalysisResultFuture result, final Resource resultResource) { final AnalysisResultSaveHandler analysisResultSaveHandler = new AnalysisResultSaveHandler(result, resultResource); try { analysisResultSaveHandler.saveOrThrow(); } catch (final SerializationException e) { // attempt to save what we can - and then rethrow final AnalysisResult safeAnalysisResult = analysisResultSaveHandler.createSafeAnalysisResult(); if (safeAnalysisResult == null) { logger.error("Serialization of result failed without any safe result elements to persist"); } else { final Map<ComponentJob, AnalyzerResult> unsafeResultElements = analysisResultSaveHandler.getUnsafeResultElements(); logger.error("Serialization of result failed with the following unsafe elements: {}", unsafeResultElements); logger.warn("Partial AnalysisResult will be persisted to filename '{}'", resultResource.getQualifiedPath()); analysisResultSaveHandler.saveWithoutUnsafeResultElements(); } // rethrow the exception regardless throw e; } }
private static void saveOrThrow(final AnalysisResult analysisResult, final Resource resource) { final SimpleAnalysisResult simpleAnalysisResult; if (analysisResult instanceof SimpleAnalysisResult) { simpleAnalysisResult = (SimpleAnalysisResult) analysisResult; } else { simpleAnalysisResult = new SimpleAnalysisResult(analysisResult.getResultMap(), analysisResult.getCreationDate()); } final OutputStream out = resource.write(); try { SerializationUtils.serialize(simpleAnalysisResult, out); } catch (final SerializationException e) { logger.error("Error serializing analysis result: " + analysisResult, e); throw e; } finally { FileHelper.safeClose(out); } }
/** * Gets a map of unsafe result elements, ie. elements that cannot be saved * because serialization fails. * * @return */ public Map<ComponentJob, AnalyzerResult> getUnsafeResultElements() { if (_unsafeResultElements == null) { _unsafeResultElements = new LinkedHashMap<>(); final Map<ComponentJob, AnalyzerResult> resultMap = _analysisResult.getResultMap(); for (final Entry<ComponentJob, AnalyzerResult> entry : resultMap.entrySet()) { final AnalyzerResult analyzerResult = entry.getValue(); try { SerializationUtils.serialize(analyzerResult, new NullOutputStream()); } catch (final SerializationException e) { _unsafeResultElements.put(entry.getKey(), analyzerResult); } } } return _unsafeResultElements; }
@Test(expected = ExecutionException.class) public void testMultiget_TranscoderExecption() throws ExecutionException, InterruptedException, TimeoutException { final Transcoder<Serializable> transcoder = new Transcoder<Serializable>() { @Override public Serializable decode(final byte[] b) { throw new SerializationException("QQQQQ YYYYY"); } @Override public byte[] encode(final Serializable t) { return SerializationUtils.serialize(t); } }; final MemcachedClient<Object, Serializable> client = createClient(transcoder); client.setAsync("meh", "its here").get(); client.getBulkAsync(Lists.newArrayList("meh", "bah")).get(1, TimeUnit.MINUTES); }
@Override public <T> T deserialize(byte[] bytes, Class<T> clazz) throws SerializationException { try { T message = clazz.newInstance(); Schema schema = getSchema(clazz); ProtostuffIOUtil.mergeFrom(bytes, message, schema); return message; } catch (Throwable e) { throw new SerializationException(e.getMessage(), e); } }
@Override public byte[] serialize(Object obj) throws SerializationException { LinkedBuffer buffer = LinkedBuffer.allocate(1024); try { Schema schema = getSchema(obj.getClass()); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Throwable e) { throw new SerializationException(e.getMessage(), e); } finally { buffer.clear(); } }
public boolean saveAttempt() { try { saveOrThrow(); return true; } catch (final SerializationException e) { return false; } }
public String encode(Object object) throws SerializationException { if (object == null) { return null; } return new String(Base64.encodeBase64(HessianZipSerializer .encode(object))); }
@Override @SuppressWarnings("unchecked") public T decode(final byte[] b) { final Template<?> template = messagePack.lookup(valueType); try { final Value value = messagePack.read(b); return (T) template.read(new Converter(messagePack, value), null); } catch (final IOException e) { throw new SerializationException("Failed to decode to type " + valueType.getTypeName(), e); } }
@Override public byte[] encode(final T t) { try { return messagePack.write(t); } catch (final IOException e) { throw new SerializationException("Failed to encode input " + t.getClass().getSimpleName() + " to type " + valueType.getTypeName(), e); } }
@Override public T decode(final byte[] b) { try { return objectMapper.readValue(b, valueType); } catch (final IOException e) { throw new SerializationException("Failed to decode to type " + valueType.getSimpleName(), e); } }
@Override public byte[] encode(final T t) { try { return objectMapper.writeValueAsBytes(t); } catch (final JsonProcessingException e) { throw new SerializationException("Failed to encode input " + t.getClass().getSimpleName() + " to type " + valueType.getSimpleName(), e); } }
@Override public Optional<Throwable> fromBytes(final byte[] bytes) { try { return Optional.<Throwable>of((Throwable) SerializationUtils.deserialize(bytes)); } catch (SerializationException | IllegalArgumentException e) { LOG.log(Level.FINE, "Unable to deserialize a Throwable.", e); return Optional.empty(); } }
/** * Constructs a JAX-WS deserializer for the specified class. * * @param clazz Class to deserialize. * @throws SerializationException if unable to construct the deserializer. */ public JaxBDeserializer(Class<T> clazz) { this.clazz = clazz; try { this.jaxbContext = JAXBContext.newInstance(clazz); } catch (JAXBException e) { throw new SerializationException( String.format("Could not construct deserializer for class: %s.", clazz), e); } }
/** * Deserializes the object. * * @throws SerializationException if we cannot deserialize the object. */ public T deserialize(Source source) { try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz); return jaxbElement.getValue(); } catch (JAXBException e) { throw new SerializationException( String.format("Could not deserialize %s object from source %s.", clazz, source), e); } }
/** * Constructs a JAX-WS serializer for the specified class. * * @param clazz Class to serialize. * @param qname A QName representing the local name for the class - this will be used as the root * tag name. * @throws SerializationException if unable to construct the serializer. */ public JaxBSerializer(Class<T> clazz, QName qname) { this.clazz = clazz; this.qname = qname; try { this.jaxbContext = JAXBContext.newInstance(clazz); } catch (JAXBException e) { throw new SerializationException( String.format( "Could not construct a serializer for class %s and QName %s.", clazz, qname), e); } }
/** * Serializes the object with the option to include or exclude the XML declaration. * * @throws SerializationException if we cannot serialize the object. */ public String serialize(T object, boolean includeXmlDeclaration) { try { Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.valueOf(!includeXmlDeclaration)); JAXBElement<T> element = new JAXBElement<T>(qname, clazz, object); StringWriter stringWriter = new StringWriter(); marshaller.marshal(element, stringWriter); return stringWriter.toString(); } catch (JAXBException e) { throw new SerializationException(String.format("Could not serialize object: %s.", object), e); } }
@Override public InvocationRequest newRequest(InvokerContext invokerContext) throws SerializationException { return new DefaultRequest(invokerContext); }
@Override public InvocationResponse newResponse() throws SerializationException { return new DefaultResponse(); }
@Override public DefaultRequest deserializeRequest(byte[] bytes) throws SerializationException { return deserialize(bytes, DefaultRequest.class); }
@Override public DefaultResponse deserializeResponse(byte[] bytes) throws SerializationException { return deserialize(bytes, DefaultResponse.class); }
@Override public byte[] serializeRequest(Object request) throws SerializationException { return serialize(request); }
@Override public byte[] serializeResponse(Object response) throws SerializationException { return serialize(response); }
public void saveOrThrow() throws SerializationException { saveOrThrow(_analysisResult, _resource); }
public Object decode(String str) throws SerializationException { if (StringUtil.isEmpty(str) == true) { return null; } return HessianZipSerializer.decode(Base64.decodeBase64(str)); }
/** * {@inheritDoc} * * @throws ClassCastException if the given <code>original</code> object is not {@link Serializable} * @throws SerializationException if serialization fails * @throws IOException if input fails during deserialization * @throws ClassNotFoundException if the <code>targetClassLoader</code> cannot find a required class */ public Object cloneObjectUsingClassLoader(Object original, ClassLoader targetClassLoader) throws ClassCastException, SerializationException, IOException, ClassNotFoundException { Assert.areNotNull(original, targetClassLoader); byte[] serializedOriginal = SerializationUtils.serialize((Serializable) original); return new ClassLoaderObjectInputStream(targetClassLoader, new ByteArrayInputStream(serializedOriginal)).readObject(); }
/** * {@inheritDoc} * * @throws ClassCastException if the given <code>original</code> object is not {@link Serializable} * @throws SerializationException if serialization fails * @throws IOException if input fails during deserialization * @throws ClassNotFoundException if the <code>targetClassLoader</code> cannot find a required class */ public Object cloneObjectUsing(ClassLoader targetLoader, Object original) throws ClassCastException, SerializationException, IOException, ClassNotFoundException { Assert.areNotNull(targetLoader, original); byte[] serializedOriginal = SerializationUtils.serialize((Serializable) original); return deserialize(serializedOriginal, targetLoader); }
public <T> T deserialize(byte[] bytes, Class<T> clazz) throws SerializationException;
public Object deserializeRequest(byte[] bytes) throws SerializationException;
public DefaultResponse deserializeResponse(byte[] bytes) throws SerializationException;
public byte[] serialize(Object body) throws SerializationException;
public byte[] serializeRequest(Object request) throws SerializationException;
public byte[] serializeResponse(Object response) throws SerializationException;
public InvocationRequest newRequest(InvokerContext invokerContext) throws SerializationException;
public InvocationResponse newResponse() throws SerializationException;
public String encode(Object object) throws SerializationException;
public Object decode(String str) throws SerializationException;