本文整理汇总了Java中com.jme3.network.serializing.Serializer.writeClassAndObject方法的典型用法代码示例。如果您正苦于以下问题:Java Serializer.writeClassAndObject方法的具体用法?Java Serializer.writeClassAndObject怎么用?Java Serializer.writeClassAndObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jme3.network.serializing.Serializer
的用法示例。
在下文中一共展示了Serializer.writeClassAndObject方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: messageToBuffer
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
/**
* Converts a message to a ByteBuffer using the Serializer
* and the (short length) + data protocol. If target is null
* then a 32k byte buffer will be created and filled.
*/
public static ByteBuffer messageToBuffer( Message message, ByteBuffer target )
{
// Could let the caller pass their own in
ByteBuffer buffer = target == null ? ByteBuffer.allocate( 32767 + 2 ) : target;
try {
buffer.position( 2 );
Serializer.writeClassAndObject( buffer, message );
buffer.flip();
short dataLength = (short)(buffer.remaining() - 2);
buffer.putShort( dataLength );
buffer.position( 0 );
return buffer;
} catch( IOException e ) {
throw new RuntimeException( "Error serializing message", e );
}
}
示例2: writeMethodCall
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
private void writeMethodCall(ByteBuffer buffer, RemoteMethodCallMessage call) throws IOException{
buffer.putShort((short)call.objectId);
buffer.putShort(call.methodId);
buffer.putShort(call.invocationId);
if (call.args == null){
buffer.put((byte)0);
}else{
buffer.put((byte)call.args.length);
// Right now it writes 0 for every null argument
// and 1 for every non-null argument followed by the serialized
// argument. For the future, using a bit set should be considered.
for (Object obj : call.args){
if (obj != null){
buffer.put((byte)0x01);
Serializer.writeClassAndObject(buffer, obj);
}else{
buffer.put((byte)0x00);
}
}
}
}
示例3: writeObject
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
if (!(object instanceof ZIPCompressedMessage)) return;
ZIPCompressedMessage zipMessage = (ZIPCompressedMessage)object;
Message message = zipMessage.getMessage();
ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
Serializer.writeClassAndObject(tempBuffer, message);
ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
ZipOutputStream zipOutput = new ZipOutputStream(byteArrayOutput);
zipOutput.setLevel(zipMessage.getLevel());
ZipEntry zipEntry = new ZipEntry("zip");
zipOutput.putNextEntry(zipEntry);
zipOutput.write(tempBuffer.array());
zipOutput.flush();
zipOutput.closeEntry();
zipOutput.close();
buffer.put(byteArrayOutput.toByteArray());
}
示例4: writeObject
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
if (!(object instanceof GZIPCompressedMessage)) return;
Message message = ((GZIPCompressedMessage)object).getMessage();
ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
Serializer.writeClassAndObject(tempBuffer, message);
ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
GZIPOutputStream gzipOutput = new GZIPOutputStream(byteArrayOutput);
gzipOutput.write(tempBuffer.array());
gzipOutput.flush();
gzipOutput.finish();
gzipOutput.close();
buffer.put(byteArrayOutput.toByteArray());
}
示例5: writeArray
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
private void writeArray(Serializer elementSerializer, ByteBuffer buffer, Object array, int dimension, int dimensionCount) throws IOException {
int length = Array.getLength(array);
if (dimension > 0) {
buffer.putInt(length);
}
// Write array data.
boolean elementsAreArrays = dimension < dimensionCount - 1;
for (int i = 0; i < length; i++) {
Object element = Array.get(array, i);
if (elementsAreArrays) {
if (element != null) writeArray(elementSerializer, buffer, element, dimension + 1, dimensionCount);
} else if (elementSerializer != null) {
elementSerializer.writeObject(buffer, element);
} else {
// Each element could be a different type. Store the class with the object.
Serializer.writeClassAndObject(buffer, element);
}
}
}
示例6: writeMethodReturn
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
private void writeMethodReturn(ByteBuffer buffer, RemoteMethodReturnMessage ret) throws IOException{
buffer.putShort(ret.invocationID);
if (ret.retVal != null){
buffer.put((byte)0x01);
Serializer.writeClassAndObject(buffer, ret.retVal);
}else{
buffer.put((byte)0x00);
}
}
示例7: write
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
public void write(Object entity, ByteBuffer out, boolean init){
FieldTable table = getTable(entity.getClass());
for (SyncFieldInfo fieldInfo : table){
if ( (init && !fieldInfo.init)
|| (!init && !fieldInfo.sync) )
continue;
Field field = fieldInfo.field;
try {
Serializer.writeClassAndObject(out, field.get(entity));
} catch (Exception ex){
ex.printStackTrace();
}
}
}
示例8: writeObject
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
@Override
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
UserData ud = (UserData) object;
Object value = ud.getValue();
byte type = UserData.getObjectType(value);
buffer.put(type);
Serializer.writeClassAndObject(buffer, value);
}
示例9: write
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
public synchronized void write(SelectableChannel channel) throws IOException {
SocketChannel socketChannel = (SocketChannel)channel;
Client client = (Client)socketChannel.keyFor(selector).attachment();
MessageQueue queue = client.getMessageQueue();
Map<Message, Short> sizeMap = new LinkedHashMap<Message, Short>();
for (Iterator<Message> it = queue.iterator(); it.hasNext();) {
Message message = it.next();
if (!message.isReliable()) continue;
int pos = writeBuffer.position();
try {
writeBuffer.position(pos + 2);
Serializer.writeClassAndObject(writeBuffer, message);
short dataLength = (short)(writeBuffer.position() - pos - 2);
writeBuffer.position(pos);
writeBuffer.putShort(dataLength);
writeBuffer.position(pos + dataLength + 2);
sizeMap.put(message, dataLength);
it.remove();
} catch (Exception bfe) {
// No problem, just write the buffer and be done with it.
writeBuffer.position(pos);
break;
}
}
writeBuffer.flip();
int written = 0;
while (writeBuffer.hasRemaining()) {
int wrote = socketChannel.write(writeBuffer);
written += wrote;
if (wrote == 0) {
break;
}
}
log.log(Level.FINE, "[{1}][TCP] Wrote {0} bytes.", new Object[]{written, label});
// Check which messages were NOT sent.
if (writeBuffer.hasRemaining()) {
for (Iterator<Map.Entry<Message, Short>> it = sizeMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<Message, Short> entry = it.next();
written -= entry.getValue();
if (written > 0) {
it.remove();
} else {
// Re add to queue.
client.getMessageQueue().add(entry.getKey());
}
}
}
if (queue.isEmpty()) {
channel.keyFor(selector).interestOps(SelectionKey.OP_READ);
}
writeBuffer.clear();
}
示例10: writeObject
import com.jme3.network.serializing.Serializer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
Map map = (Map)object;
int length = map.size();
buffer.putInt(length);
if (length == 0) return;
Set<Entry> entries = map.entrySet();
Iterator<Entry> it = entries.iterator();
Entry entry = it.next();
Class keyClass = entry.getKey().getClass();
Class valClass = entry.getValue().getClass();
while (it.hasNext()) {
entry = it.next();
if (entry.getKey().getClass() != keyClass){
keyClass = null;
if (valClass == null)
break;
}
if (entry.getValue().getClass() != valClass){
valClass = null;
if (keyClass == null)
break;
}
}
boolean uniqueKeys = keyClass == null;
boolean uniqueVals = valClass == null;
int flags = 0;
if (!uniqueKeys) flags |= 0x01;
if (!uniqueVals) flags |= 0x02;
buffer.put( (byte) flags );
Serializer keySerial = null, valSerial = null;
if (!uniqueKeys){
Serializer.writeClass(buffer, keyClass);
keySerial = Serializer.getSerializer(keyClass);
}
if (!uniqueVals){
Serializer.writeClass(buffer, valClass);
valSerial = Serializer.getSerializer(valClass);
}
it = entries.iterator();
while (it.hasNext()) {
entry = it.next();
if (uniqueKeys){
Serializer.writeClassAndObject(buffer, entry.getKey());
}else{
keySerial.writeObject(buffer, entry.getKey());
}
if (uniqueVals){
Serializer.writeClassAndObject(buffer, entry.getValue());
}else{
valSerial.writeObject(buffer, entry.getValue());
}
}
}