Example usage for org.apache.commons.lang3 SerializationUtils serialize

List of usage examples for org.apache.commons.lang3 SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SerializationUtils serialize.

Prototype

public static byte[] serialize(final Serializable obj) 

Source Link

Document

Serializes an Object to a byte array for storage/serialization.

Usage

From source file:com.iveely.computing.component.TupleBuffer.java

/**
 * Pop cache to bytes./*w  w w  .  j av  a 2 s .  c o  m*/
 *
 * @param index Index of client.
 * @return Bytes data.
 */
public byte[] pop(int index) {
    if (isEmpty(index)) {
        return new byte[0];
    }
    byte[] ret = SerializationUtils.serialize(this.list.get(index));
    this.list.remove(index);
    return ret;
}

From source file:com.uber.hoodie.common.util.collection.converter.HoodieRecordConverter.java

@Override
public byte[] getBytes(HoodieRecord hoodieRecord) {
    try {//w w  w. ja v a2 s  .c  om
        // Need to initialize this to 0 bytes since deletes are handled by putting an empty record in HoodieRecord
        byte[] val = new byte[0];
        if (hoodieRecord.getData().getInsertValue(schema).isPresent()) {
            val = HoodieAvroUtils
                    .avroToBytes((GenericRecord) hoodieRecord.getData().getInsertValue(schema).get());
        }
        byte[] currentLocation = hoodieRecord.getCurrentLocation() != null
                ? SerializationUtils.serialize(hoodieRecord.getCurrentLocation())
                : new byte[0];
        byte[] newLocation = hoodieRecord.getNewLocation().isPresent()
                ? SerializationUtils.serialize((HoodieRecordLocation) hoodieRecord.getNewLocation().get())
                : new byte[0];

        // Triple<Pair<RecordKey, PartitionPath>, Pair<oldLocation, newLocation>, data>
        Triple<Pair<String, String>, Pair<byte[], byte[]>, byte[]> data = Triple.of(
                Pair.of(hoodieRecord.getKey().getRecordKey(), hoodieRecord.getKey().getPartitionPath()),
                Pair.of(currentLocation, newLocation), val);
        return SerializationUtils.serialize(data);
    } catch (IOException io) {
        throw new HoodieNotSerializableException("Cannot serialize value to bytes", io);
    }
}

From source file:com.iveely.framework.net.AsynClient.java

/**
 * Send message to server./* w w  w.j  a  v  a2 s .com*/
 */
public boolean send(Packet packet) {
    ConnectFuture future = this.connector.connect(new InetSocketAddress(this.ipAddress, this.port));
    try {
        byte[] msg = SerializationUtils.serialize(packet);
        future.awaitUninterruptibly();
        future.getSession().write(msg);
        return true;
    } catch (RuntimeIoException e) {
        e.printStackTrace();
        if (e.getCause() instanceof ConnectException) {
            try {
                if (future.isConnected()) {
                    future.getSession().close();
                }
            } catch (RuntimeIoException e1) {
                e1.printStackTrace();
            }
        }
    }
    return false;
}

From source file:EJBs.Operations.java

public String cartCheckout(String username, HashMap<Products, Integer> cart, double total) {
    try {/*from ww  w. j a va  2 s  .  com*/
        for (Map.Entry<Products, Integer> entry : cart.entrySet()) {
            System.out.println(entry.getKey() + "/" + entry.getValue());
            Products product = em.find(Products.class, entry.getKey().getName());
            product.setStock(product.getStock() - cart.get(product));
            em.persist(product);
        }
        byte[] cartData = SerializationUtils.serialize(cart);
        Orders newOrd = new Orders(username, cartData, total);
        em.persist(newOrd);
        String oid = "" + em.createQuery("SELECT max(o.id) FROM Orders o where o.username=:username")
                .setParameter("username", username).getSingleResult();
        return oid;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return "";
    }
}

From source file:com.dangdang.ddframe.job.cloud.executor.TaskExecutorTest.java

@Test
public void assertRegisteredWithoutData() {
    // CHECKSTYLE:OFF
    HashMap<String, String> data = new HashMap<>(4, 1);
    // CHECKSTYLE:ON
    data.put("event_trace_rdb_driver", "org.h2.Driver");
    data.put("event_trace_rdb_url", "jdbc:h2:mem:test_executor");
    data.put("event_trace_rdb_username", "sa");
    data.put("event_trace_rdb_password", "");
    ExecutorInfo executorInfo = ExecutorInfo.newBuilder()
            .setExecutorId(Protos.ExecutorID.newBuilder().setValue("test_executor"))
            .setCommand(Protos.CommandInfo.getDefaultInstance())
            .setData(ByteString.copyFrom(SerializationUtils.serialize(data))).build();
    taskExecutor.registered(executorDriver, executorInfo, frameworkInfo, slaveInfo);
}

From source file:com.francetelecom.clara.cloud.paas.activation.v1.async.AmqpTaskHandler.java

@Override
public TaskStatus handleRequest(R request, final String communicationId) {
    log.trace("(0) handleRequest {}", request.toString());
    // callback delegation
    final TaskStatus taskStatus = taskHandlerCallback.handleRequest(request);

    if (taskStatus.isComplete()) {
        log.trace("(1) task is complete. no need to poll. Sends task Status to reply queue");
        MessageProperties props;//from  w  ww  .ja va  2 s . com
        props = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT)
                .setMessageId(UUID.randomUUID().toString()).setCorrelationId(communicationId.getBytes())
                .build();
        Message message = MessageBuilder.withBody(SerializationUtils.serialize(taskStatus)).andProperties(props)
                .build();
        amqpReplyTemplate.send(message);
        log.trace("(2) message sent to reply queue");
    } else {
        log.trace(
                "(1) handleRequest status : {} send in amqp queue a 'retry' request used to poll backend service for",
                taskStatus.toString());
        MessageProperties props;
        props = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT)
                .setMessageId(UUID.randomUUID().toString()).setCorrelationId(communicationId.getBytes())
                .setHeader("retryCount", 1).build();
        Message message = MessageBuilder.withBody(SerializationUtils.serialize(taskStatus)).andProperties(props)
                .build();
        amqpRequestTemplate.send(message);
        log.trace("(2) handleRequest send amqp message ended");

    }

    return taskStatus;
}

From source file:com.splicemachine.derby.stream.output.direct.DirectTableWriterBuilder.java

public String base64Encode() {
    return Base64.encodeBase64String(SerializationUtils.serialize(this));
}

From source file:com.omertron.themoviedbapi.AbstractTests.java

/**
 * Write out the object to a file//from   w  w  w .  ja va  2  s  .c o  m
 *
 * @param object
 * @param filename
 * @return
 */
private static boolean writeObject(final Serializable object, final String baseFilename) {
    String filename = baseFilename + FILENAME_EXT;
    File serFile = new File(filename);

    if (serFile.exists()) {
        serFile.delete();
    }

    try {
        byte[] serObject = SerializationUtils.serialize(object);
        FileUtils.writeByteArrayToFile(serFile, serObject);
        return true;
    } catch (IOException ex) {
        LOG.info("Failed to write object to '{}': {}", filename, ex.getMessage(), ex);
        return false;
    }
}

From source file:com.sangupta.shire.util.WebResponseCacheInterceptor.java

/**
 * @see com.sangupta.jerry.http.WebInvocationInterceptor#afterInvocation(com.sangupta.jerry.http.WebResponse)
 *///  w w w .  java  2 s .  c  o  m
@Override
public WebResponse afterInvocation(WebResponse response) {
    // if we have reached here, this means that the response needs
    // to be saved in the cache dir
    File cache = getCacheFile(this.url.get());
    if (cache != null) {
        try {
            FileUtils.writeByteArrayToFile(cache, SerializationUtils.serialize(response));
        } catch (IOException e) {
            // eat up
        }
    }

    return response;
}

From source file:fredboat.db.entity.SearchResult.java

public SearchResult(AudioPlayerManager playerManager, SearchUtil.SearchProvider provider, String searchTerm,
        AudioPlaylist searchResult) {//  ww  w .j  a  v a2 s.  co m
    this.searchResultId = new SearchResultId(provider, searchTerm);
    this.timestamp = System.currentTimeMillis();
    this.serializedSearchResult = SerializationUtils
            .serialize(new SerializableAudioPlaylist(playerManager, searchResult));
}