public static RobotData getRobotData(int first, int second) { int id = first & 0x7FFF; MapLocation location = Serializer.decodeMapLocation(second >>> 7); int health = first >>> 15; RobotType type = Serializer.decodeRobotType((second >>> 3) & 0xF); return new RobotData(id, location, health, type); }
public static void main(final String[] args) throws IOException { final YAMLConfig cfg = YAML.config(); final Serializer s = new SerializerImpl(new EmitterImpl(System.out, cfg), new ResolverImpl(), cfg); s.open(); final Representer r = new RepresenterImpl(s, cfg); final Map test1 = new HashMap(); final List test1Val = new LinkedList(); test1Val.add("hello"); test1Val.add(Boolean.TRUE); test1Val.add(new Integer(31337)); test1.put("val1", test1Val); final List test2Val = new ArrayList(); test2Val.add("hello"); test2Val.add(Boolean.FALSE); test2Val.add(new Integer(31337)); test1.put("val2", test2Val); test1.put("afsdf", "hmm"); TestJavaBean bean1 = new TestJavaBean(); bean1.setName("Ola"); bean1.setSurName("Bini"); bean1.setAge(24); test1.put(new Integer(25), bean1); r.represent(test1); s.close(); }
@Override @SuppressWarnings("unchecked") public void writeObject(Object obj) throws IOException { if (obj == null) { writeByte(REF_NULL); } else { int index = context.indexOf(obj); if (index < 0) { Serializer serializer = Repository.get(obj.getClass()); if (serializer.uid < 0) { writeByte((byte) serializer.uid); } else { writeByte(REF_EMBEDDED); writeObject(serializer); } context.put(obj); serializer.write(obj, this); } else if (index <= 0xffff) { writeByte(REF_RECURSIVE); writeShort(index); } else { writeByte(REF_RECURSIVE2); writeInt(index); } } }
private void writeWayNodes(List<Integer> waynodes, int compressionType, MappedByteBuffer buffer) { if (!waynodes.isEmpty() && waynodes.size() % 2 == 0) { Iterator<Integer> waynodeIterator = waynodes.iterator(); buffer.putInt(waynodeIterator.next()); buffer.putInt(waynodeIterator.next()); while (waynodeIterator.hasNext()) { switch (compressionType) { case 0: buffer.putInt(waynodeIterator.next().intValue()); buffer.putInt(waynodeIterator.next().intValue()); break; case 1: buffer.put(Serializer.getSignedThreeBytes(waynodeIterator.next().intValue())); buffer.put(Serializer.getSignedThreeBytes(waynodeIterator.next().intValue())); break; case 2: buffer.putShort(waynodeIterator.next().shortValue()); buffer.putShort(waynodeIterator.next().shortValue()); break; case 3: buffer.put(waynodeIterator.next().byteValue()); buffer.put(waynodeIterator.next().byteValue()); break; } } } }
@Test public void demonstrateTheProgrammaticAPI() { // #programmatic ActorSystem system = ActorSystem.create("example"); // Get the Serialization Extension Serialization serialization = SerializationExtension.get(system); // Have something to serialize String original = "woohoo"; // Find the Serializer for it Serializer serializer = serialization.findSerializerFor(original); // Turn it into bytes byte[] bytes = serializer.toBinary(original); // Turn it back into an object, // the nulls are for the class manifest and for the classloader String back = (String) serializer.fromBinary(bytes); // Voilá! assertEquals(original, back); // #programmatic system.shutdown(); }
public GensonBuilder withSerializers(Serializer<?>... serializer) { for (Serializer<?> s : serializer) { Type typeOfConverter = TypeUtil.typeOf(0, TypeUtil.lookupGenericType(Serializer.class, s.getClass())); typeOfConverter = TypeUtil.expandType(typeOfConverter, s.getClass()); registerSerializer(s, typeOfConverter); } return this; }
private void addDefaultSerializers(List<? extends Serializer<?>> serializers) { if (serializers != null) { for (Serializer<?> serializer : serializers) { Type typeOfConverter = TypeUtil.typeOf(0, TypeUtil.lookupGenericType(Serializer.class, serializer.getClass())); typeOfConverter = TypeUtil.expandType(typeOfConverter, serializer.getClass()); if (!serializersMap.containsKey(typeOfConverter)) serializersMap.put(typeOfConverter, serializer); } } }
/** Writes any object to the output stream. */ public void writeObject(Object object) throws IOException { if (object == null) { writeNull(); return; } Serializer serializer; serializer = findSerializerFactory().getSerializer(object.getClass()); serializer.writeObject(object, this); }
@SuppressWarnings("unchecked") @Override public <R> SerializedObject<R> serializeMetaData( Serializer serializer, Class<R> expectedRepresentation) { if (serializer.equals(serializedMetaData.getSerializer())) { final SerializedObject serializedObject = serializedMetaData.getSerializedObject(); return CONVERTER_FACTORY .getConverter(serializedObject.getContentType(), expectedRepresentation) .convert(serializedObject); } return serializer.serialize(serializedMetaData.getObject(), expectedRepresentation); }
public static synchronized void managerDatanodesCreated(String hostName, NodeStatus nodeLocal) { try { if (nodeLocal.getNodesResponding().contains(hostName)) { String path = ZkConf.DATANODES_PATH + "/" + HOSTNAME; nodeLocal.getNodesResponding().remove(nodeLocal.getNodesResponding().indexOf(hostName)); zk.setData(path, Serializer.fromObject(nodeLocal), -1); } if (HOSTNAME.equals(hostName)) { loadDada(); if (managerDatanodesResponding.containsKey(hostName)) { managerDatanodesResponding.remove(hostName); zk.setData( ZkConf.MANAGER_NODES_RESPONDING, Serializer.fromObject((Serializable) managerDatanodesResponding), -1); } if (datanodesDesconnected.contains(hostName)) { datanodesDesconnected.remove(hostName); if (historicSendDatanodesDesconnected.containsKey(hostName)) { historicSendDatanodesDesconnected.remove(hostName); } zk.setData( ZkConf.DATANODES_DESCONNECTED, Serializer.fromObject((Serializable) datanodesDesconnected), -1); zk.setData( ZkConf.HISTORIC_SEND, Serializer.fromObject((Serializable) historicSendDatanodesDesconnected), -1); } if (datanodesConnected.size() > 1) { new SupportReplyText().checkRepliesText(datanodesReplyReceive); new SupportReplyImage().checkRepliesImage(datanodesReplyReceive); } clear(); } } catch (KeeperException e) { LOG.error(e.getMessage(), e); } catch (InterruptedException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } }
public static void save( int routineStart, int routineDefense, int routinePosition, int routineGoal, int routineFinish) { if (serializer.isFinished()) { serializer.setData( new RoutineData( routineStart, routineDefense, routinePosition, routineGoal, routineFinish)); serializer.run(); } }
public void register(Class type, Serializer serializer) { if (type == null) { throw new IllegalArgumentException("Class type information is required!"); } if (serializer.getTypeId() <= 0) { throw new IllegalArgumentException( "Type id must be positive! Current: " + serializer.getTypeId() + ", Serializer: " + serializer); } safeRegister(type, createSerializerAdapter(serializer)); }
private void writeSubfileMetaDataToContainerHeader( int i, long startIndexOfSubfile, long subfileSize) { // HEADER META DATA FOR SUB FILE // write zoom interval configuration to header byte minZoomCurrentInterval = dataStore.getZoomIntervalConfiguration().getMinZoom(i); byte maxZoomCurrentInterval = dataStore.getZoomIntervalConfiguration().getMaxZoom(i); byte baseZoomCurrentInterval = dataStore.getZoomIntervalConfiguration().getBaseZoom(i); bufferZoomIntervalConfig.put(baseZoomCurrentInterval); bufferZoomIntervalConfig.put(minZoomCurrentInterval); bufferZoomIntervalConfig.put(maxZoomCurrentInterval); bufferZoomIntervalConfig.put(Serializer.getFiveBytes(startIndexOfSubfile)); bufferZoomIntervalConfig.put(Serializer.getFiveBytes(subfileSize)); }
public void registerGlobal(final Serializer serializer) { SerializerAdapter adapter = createSerializerAdapter(serializer); if (!global.compareAndSet(null, adapter)) { throw new IllegalStateException("Global serializer is already registered!"); } SerializerAdapter current = idMap.putIfAbsent(serializer.getTypeId(), adapter); if (current != null && current.getImpl().getClass() != adapter.getImpl().getClass()) { global.compareAndSet(adapter, null); throw new IllegalStateException( "Serializer [" + current.getImpl() + "] has been already registered for type-id: " + serializer.getTypeId()); } }
@Test public void testCustomTypeAdapter() { serializer = serializer.withTypeAdapter( StringObject.class, new JsonSerializer<StringObject>() { @Override public JsonElement serialize( StringObject strObj, Type type, JsonSerializationContext jsc) { return new JsonPrimitive(strObj.toString() + ", with custom serialization!"); } }); Bond.obs("StringObject", serializer.serialize(new StringObject("foobar"))).spy(); }
/** * delete from the database * * @param key * @throws Exception */ public void delete(Object key) throws Exception { Serializer serializer = new Serializer(); byte[] keyBytes = serializer.serialize(key); final DatabaseEntry keyEntry = new DatabaseEntry(keyBytes); final Transaction transaction = myEnv.beginTransaction(null, null); final OperationStatus res = myDatabase.delete(transaction, keyEntry); if (res != OperationStatus.SUCCESS) { throw new Exception("Error retrieving from database"); } transaction.commit(); return; }
/** Wait until there more data to process */ public synchronized void sendAndWaitUntilNextUdpMessage(UDPMessage message) throws Exception { byte[] response = Serializer.serialize(message); DatagramPacket reply = new DatagramPacket( response, response.length, receivedDatagram.getAddress(), receivedDatagram.getPort()); try { // lock to wait for next DatagramPacket which is the answer of this reply waitForNextDatagram = true; aSocket.send(reply); // wait for the response before continuing while (waitForNextDatagram) { try { this.wait(); } catch (Exception e) { } } waitForNextDatagram = true; } catch (Exception e) { // handled by the parent function throw e; } }
/** Wait until there more data to process */ public int sendToAll(UDPMessage message) { byte[] response; try { response = Serializer.serialize(message); } catch (Exception e) { System.out.println("sendToAll Serializer.serialize(message) Exception: " + e.getMessage()); return 0; } int nCount = 0; for (Iterator<String> iterator = Env.getListMachineName(); iterator.hasNext(); ) { String machineName = iterator.next(); for (ServerInfo replica : Env.getReplicaServerInfoList(machineName)) { DatagramPacket reply = new DatagramPacket(response, response.length, replica.getAddress()); try { aSocket.send(reply); nCount++; } catch (Exception e) { // handled by the parent function System.out.println("sendToAll Exception: " + e.getMessage()); } } } return nCount; }
/** Saves the displayed text. */ private void save() { final BaseXFileChooser fc = new BaseXFileChooser(SAVE_AS, gui.gopts.get(GUIOptions.WORKPATH), gui).suffix(IO.XMLSUFFIX); final IO file = fc.select(Mode.FSAVE); if (file == null) return; gui.gopts.set(GUIOptions.WORKPATH, file.path()); gui.cursor(CURSORWAIT, true); final MainOptions opts = gui.context.options; final int mh = opts.get(MainOptions.MAXHITS); opts.set(MainOptions.MAXHITS, -1); opts.set(MainOptions.CACHEQUERY, false); try (final PrintOutput out = new PrintOutput(file.toString())) { if (cmd != null) { cmd.execute(gui.context, out); } else if (ns != null) { ns.serialize(Serializer.get(out)); } else { final byte[] txt = text.getText(); for (final byte t : txt) if (t < 0 || t > ' ' || ws(t)) out.write(t); } } catch (final IOException ex) { BaseXDialog.error(gui, Util.info(FILE_NOT_SAVED_X, file)); } finally { opts.set(MainOptions.MAXHITS, mh); opts.set(MainOptions.CACHEQUERY, true); gui.cursor(CURSORARROW, true); } }
@Override public void load(Class<? extends AEntity> cls, String alias) { if (!versionChecked) checkVersion(); aliases.put(cls, alias); Map<String, AEntity> entities = new HashMap<String, AEntity>(); data.put((Class<AEntity>) cls, entities); beanSerializer.setAlias(alias, cls); File f = new File(dir + "/" + alias); LOG.info("Loading entities:", alias); File[] files = f.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.getName().equals(CLUSTER_FILE_NAME)) continue; try { loadObject(file, entities, cls, alias); } catch (Throwable ex) { throw new RuntimeException("Loading object from " + file + " failed", ex); } } } // LOG.info(" Loaded entities:", alias, count); }
/** * Lista os {@link DataNode}s conectados no momento ao ClusterService. * * @return datanodes * @throws KeeperException * @throws InterruptedException * @throws IOException */ public static List<NodeStatus> getDatanodesConnected() { List<NodeStatus> datanodes = new ArrayList<NodeStatus>(); try { List<String> nodesIds = zk.getChildren(ZkConf.DATANODES_PATH, true); LOG.info("Cluster com " + nodesIds.size() + " datanode(s) ativo(s) no momento."); for (String hostName : nodesIds) { String path = ZkConf.DATANODES_PATH + "/" + hostName; byte[] bytes = zk.getData(path, true, null); NodeStatus node = (NodeStatus) Serializer.toObject(bytes); datanodes.add(node); } } catch (ClassNotFoundException e) { LOG.error(e.getMessage(), e); } catch (KeeperException e) { LOG.error(e.getMessage(), e); } catch (InterruptedException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } return datanodes; }
@Test public void ln_serialization() throws IOException { HTreeMap.LinkedNode n = new HTreeMap.LinkedNode(123456, 123L, 456L); DataOutput2 out = new DataOutput2(); Serializer ln_serializer = new HTreeMap(recman, true).LN_SERIALIZER; ln_serializer.serialize(out, n); DataInput2 in = swap(out); HTreeMap.LinkedNode n2 = (HTreeMap.LinkedNode) ln_serializer.deserialize(in, -1); assertEquals(123456, n2.next); assertEquals(123L, n2.key); assertEquals(456L, n2.value); }
public static synchronized void managerNodesChanged(String hostName, NodeStatus nodeLocal) { try { String path = ZkConf.DATANODES_PATH + "/" + hostName; List<String> listSend = getListDatanodesSendReply(hostName); if (listSend.contains(HOSTNAME)) { zk.exists(path, true); } if (hostName.equals(HOSTNAME)) { managerDatanodesResponding = getManagerDatanodesResponding(); byte[] bytes = zk.getData(path, true, null); List<String> nodesResponding = new ArrayList<String>(); NodeStatus datanode = (NodeStatus) Serializer.toObject(bytes); managerDatanodesResponding.put(datanode.getHostname(), datanode.getNodesResponding()); nodesResponding = managerDatanodesResponding.get(datanode.getHostname()); if (nodesResponding.isEmpty()) managerDatanodesResponding.remove(datanode.getHostname()); nodeLocal.setNodesResponding(datanode.getNodesResponding()); zk.setData( ZkConf.MANAGER_NODES_RESPONDING, Serializer.fromObject((Serializable) managerDatanodesResponding), -1); } if (nodeLocal.getNodesResponding().size() > 0) { LOG.info("Este datanode esta respondendo pelo(s): " + nodeLocal.getNodesResponding()); } else { LOG.info("Este datanode não está respondendo por nenhum outro."); } clear(); } catch (KeeperException e) { LOG.error(e.getMessage(), e); } catch (InterruptedException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } catch (ClassNotFoundException e) { LOG.error(e.getMessage(), e); } }
@Override public void serialize(NamespaceDescriptor ns) { super.serialize(ns); if (isRootNs(ns)) { return; } sb.append("."); }
/** * Returns a serializer for the given output stream. Optional output declarations within the query * will be included in the serializer instance. * * @param os output stream * @return serializer instance * @throws IOException query exception * @throws QueryException query exception */ public Serializer getSerializer(final OutputStream os) throws IOException, QueryException { compile(); try { return Serializer.get(os, qc.serParams()); } catch (final QueryIOException ex) { throw ex.getCause(); } }
private void sendResponse(long requestId, Object value, Class<?> declaredType) throws IOException { DataOutputStream out = newFlusher(); out.writeByte(RESPONSE); out.writeLong(requestId); if (value == null) { out.writeBoolean(false); } else { out.writeBoolean(true); Class<?> clazz = value.getClass(); out.writeUTF(clazz.getName()); Serializer<Object> s = serializerFor(clazz, declaredType); s.serialize(out, value); } out.writeBoolean(false); out.flush(); }
private <T> void registerSerializer(Serializer<T> serializer, Type type) { if (serializersMap.containsKey(type)) throw new IllegalStateException( "Can not register serializer " + serializer.getClass() + ". A custom serializer is already registered for type " + type); serializersMap.put(type, serializer); }
private <T> Response<T> sendRequest( RequestType type, SlaveContext slaveContext, Serializer serializer, Deserializer<T> deserializer) { // TODO Refactor, break into smaller methods Triplet<Channel, ChannelBuffer, ByteBuffer> channelContext = null; try { // Send 'em over the wire channelContext = getChannel(); Channel channel = channelContext.first(); ChannelBuffer buffer = channelContext.second(); buffer.clear(); buffer = new ChunkingChannelBuffer(buffer, channel, MAX_FRAME_LENGTH); buffer.writeByte(type.ordinal()); if (type.includesSlaveContext()) { writeSlaveContext(buffer, slaveContext); } serializer.write(buffer, channelContext.third()); if (buffer.writerIndex() > 0) { channel.write(buffer); } // Read the response @SuppressWarnings("unchecked") BlockingReadHandler<ChannelBuffer> reader = (BlockingReadHandler<ChannelBuffer>) channel.getPipeline().get("blockingHandler"); final Triplet<Channel, ChannelBuffer, ByteBuffer> finalChannelContext = channelContext; DechunkingChannelBuffer dechunkingBuffer = new DechunkingChannelBuffer(reader) { @Override protected ChannelBuffer readNext() { ChannelBuffer result = super.readNext(); if (result == null) { channelPool.dispose(finalChannelContext); throw new HaCommunicationException("Channel has been closed"); } return result; } }; T response = deserializer.read(dechunkingBuffer); TransactionStream txStreams = type.includesSlaveContext() ? readTransactionStreams(dechunkingBuffer) : TransactionStream.EMPTY; return new Response<T>(response, txStreams); } catch (ClosedChannelException e) { channelPool.dispose(channelContext); throw new HaCommunicationException(e); } catch (IOException e) { throw new HaCommunicationException(e); } catch (InterruptedException e) { throw new HaCommunicationException(e); } catch (Exception e) { throw new HaCommunicationException(e); } }
public boolean commitToSerializer() { // Only do this if Serializer is set if (serializer != null) { serializer.gatherData(this); size = 0; objects = new Vector<BSBMResource>(maxSize); return true; } else return false; }
private Builder<T> validateHeader() { if (arg2 != null) { headers = null; return this; } arg2 = serializer.encodeHeaders(this.headers, argScheme); return this; }