本文整理汇总了Java中java.io.Externalizable.writeExternal方法的典型用法代码示例。如果您正苦于以下问题:Java Externalizable.writeExternal方法的具体用法?Java Externalizable.writeExternal怎么用?Java Externalizable.writeExternal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.Externalizable
的用法示例。
在下文中一共展示了Externalizable.writeExternal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeObjectToFile
import java.io.Externalizable; //导入方法依赖的package包/类
public static boolean writeObjectToFile(final File saveFile, final Externalizable data) {
ObjectOutput out;
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile, true)));
data.writeExternal(out);
// out.writeObject(data);
out.close();
} catch (Exception e) {
Log.e(LOG_TAG, "Error writing bookmark to file", e);
return false;
}
return true;
}
示例2: write
import java.io.Externalizable; //导入方法依赖的package包/类
/**
* Writes an object to the ObjectOutput stream.
* If possible, we will send over a magic number instead of the class name
* so that we transfer less amount of data.
* @param inst - an object instance to be serialized, can not be null
* @param out - the ObjectOutput stream we will write the serialized data to
*/
public static void write(Externalizable inst, ObjectOutput out) throws IOException {
boolean is_null=(inst == null);
try {
// if inst is a null value we write this first
out.writeBoolean(is_null);
if(is_null)
return;
//find out if we have a magic number for this class
int magic=mConfigurator.getMagicNumber(inst.getClass());
//-1 means no magic number otherwise we have one
if(magic != -1) {
//true means we use a magic number
out.writeBoolean(true);
//write the magic number
out.writeInt(magic);
}
else {
//we don't have a magic number
out.writeBoolean(false);
//write the classname instead
out.writeUTF(inst.getClass().getName());
}//end if
//write the object data
inst.writeExternal(out);
}
catch(Exception x) {
if(x instanceof IOException)
throw (IOException)x;
else
throw new java.io.IOException(x.toString());
}
}
示例3: onTransact
import java.io.Externalizable; //导入方法依赖的package包/类
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
switch(code) {
case TRANSACT_methodWithExternalizableReturn: {
data.enforceInterface(this.getInterfaceDescriptor());
final Externalizable returnValue = delegate.methodWithExternalizableReturn();
reply.writeNoException();
if (returnValue == null) {
reply.writeByte((byte) -1);
} else {
reply.writeByte((byte) 0);
ObjectOutputStream objectOutputStream = null;
try {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(arrayOutputStream);
returnValue.writeExternal(objectOutputStream);
objectOutputStream.flush();
reply.writeByteArray(arrayOutputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Failed to serialize net.sf.aidl2.SomeExternalizable", e);
} finally {
AidlUtil.shut(objectOutputStream);
}
}
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
示例4: writeExternalizableList
import java.io.Externalizable; //导入方法依赖的package包/类
public static void writeExternalizableList(final List<Externalizable> list, final ObjectOutput out)
throws IOException {
if (list == null) {
out.writeInt(-1);
} else {
final int listSize = list.size();
out.writeInt(listSize);
for (final Externalizable aList : list) {
aList.writeExternal(out);
}
}
}
示例5: write
import java.io.Externalizable; //导入方法依赖的package包/类
@Override
public void write(Externalizable object) {
int pos = activePosition;
try {
object.writeExternal(this);
} catch (IOException e) {
throw new RuntimeException(e);
}
assert(Pipe.validateVarLength(this.backingPipe, length(this)));
assert(collectObjectSizeData(activePosition-pos));
}
示例6: serializeObject
import java.io.Externalizable; //导入方法依赖的package包/类
public static byte[] serializeObject(Externalizable object)
throws Exception {
ByteArrayOutputStream baos;
ObjectOutputStream oos = null;
byte[] res = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
object.writeExternal(oos);
oos.flush();
res = baos.toByteArray();
} catch (Exception ex) {
System.out.println("Error serializing object" + ex);
throw ex;
} finally {
try {
if (oos != null)
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return res;
}
示例7: write
import java.io.Externalizable; //导入方法依赖的package包/类
@Override
public void write(Externalizable externalizable, BufferOutput buffer, Serializer serializer) {
try {
externalizable.writeExternal(new BufferObjectOutput(buffer, serializer));
} catch (IOException e) {
throw new SerializationException("failed to serialize externalizable type: " + externalizable.getClass(), e);
}
}
示例8: toBytes
import java.io.Externalizable; //导入方法依赖的package包/类
public static byte[] toBytes(Externalizable o) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
o.writeExternal(out);
out.flush();
return baos.toByteArray();
}
示例9: externalize
import java.io.Externalizable; //导入方法依赖的package包/类
private void externalize(final Externalizable original, final Externalizable copy) throws IOException, ClassNotFoundException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(baos);
original.writeExternal(out);
out.close();
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream in = new ObjectInputStream(bais);
copy.readExternal(in);
}
示例10: marshal
import java.io.Externalizable; //导入方法依赖的package包/类
public void marshal(Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
try {
Externalizable externalizable = (Externalizable) source;
CustomObjectOutputStream.StreamCallback callback = new CustomObjectOutputStream.StreamCallback() {
public void writeToStream(Object object) {
if (object == null) {
writer.startNode("null");
writer.endNode();
} else {
ExtendedHierarchicalStreamWriterHelper.startNode(writer, mapper.serializedClass(object.getClass()), object.getClass());
context.convertAnother(object);
writer.endNode();
}
}
public void writeFieldsToStream(Map fields) {
throw new UnsupportedOperationException();
}
public void defaultWriteObject() {
throw new UnsupportedOperationException();
}
public void flush() {
writer.flush();
}
public void close() {
throw new UnsupportedOperationException("Objects are not allowed to call ObjectOutput.close() from writeExternal()");
}
};
CustomObjectOutputStream objectOutput = CustomObjectOutputStream.getInstance(context, callback);
externalizable.writeExternal(objectOutput);
objectOutput.popCallback();
} catch (IOException e) {
throw new ConversionException("Cannot serialize " + source.getClass().getName() + " using Externalization", e);
}
}
示例11: write
import java.io.Externalizable; //导入方法依赖的package包/类
@Override
public void write(Kryo kryo, Output output, Externalizable object) {
KryoObjectOutput koo = new KryoObjectOutput(output, kryo);
try {
object.writeExternal(koo);
} catch (IOException e) {
//shouldn't happen
throw new RuntimeException(e);
}
}
示例12: marshal
import java.io.Externalizable; //导入方法依赖的package包/类
@Override
public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
try {
final Externalizable externalizable = (Externalizable)source;
final CustomObjectOutputStream.StreamCallback callback = new CustomObjectOutputStream.StreamCallback() {
@Override
public void writeToStream(final Object object) {
if (object == null) {
writer.startNode("null");
writer.endNode();
} else {
ExtendedHierarchicalStreamWriterHelper.startNode(writer, mapper.serializedClass(object
.getClass()), object.getClass());
context.convertAnother(object);
writer.endNode();
}
}
@Override
public void writeFieldsToStream(final Map<String, Object> fields) {
throw new UnsupportedOperationException();
}
@Override
public void defaultWriteObject() {
throw new UnsupportedOperationException();
}
@Override
public void flush() {
writer.flush();
}
@Override
public void close() {
throw new UnsupportedOperationException(
"Objects are not allowed to call ObjectOutput.close() from writeExternal()");
}
};
@SuppressWarnings("resource")
final CustomObjectOutputStream objectOutput = CustomObjectOutputStream.getInstance(context, callback);
externalizable.writeExternal(objectOutput);
objectOutput.popCallback();
} catch (final IOException e) {
throw new ConversionException("Cannot serialize " + source.getClass().getName() + " using Externalization",
e);
}
}
示例13: outputObject
import java.io.Externalizable; //导入方法依赖的package包/类
private void outputObject(final Object obj) throws IOException{
currentObject = obj;
Class currclass = obj.getClass();
/* Get the Class descriptor for this class,
* Throw a NotSerializableException if there is none.
*/
currentClassDesc = ObjectStreamClass.lookup(currclass);
if (currentClassDesc == null) {
// XXX I18N, Logging needed.
throw new NotSerializableException(currclass.getName());
}
/* If the object is externalizable,
* call writeExternal.
* else do Serializable processing.
*/
if (currentClassDesc.isExternalizable()) {
// Write format version
orbStream.write_octet(streamFormatVersion);
Externalizable ext = (Externalizable)obj;
ext.writeExternal(this);
} else {
/* The object's classes should be processed from supertype to subtype
* Push all the clases of the current object onto a stack.
* Remember the stack pointer where this set of classes is being pushed.
*/
int stackMark = classDescStack.size();
try {
ObjectStreamClass next;
while ((next = currentClassDesc.getSuperclass()) != null) {
classDescStack.push(currentClassDesc);
currentClassDesc = next;
}
/*
* For currentClassDesc and all the pushed class descriptors
* If the class is writing its own data
* set blockData = true; call the class writeObject method
* If not
* invoke either the defaultWriteObject method.
*/
do {
WriteObjectState oldState = writeObjectState;
try {
setState(NOT_IN_WRITE_OBJECT);
if (currentClassDesc.hasWriteObject()) {
invokeObjectWriter(currentClassDesc, obj );
} else {
defaultWriteObjectDelegate();
}
} finally {
setState(oldState);
}
} while (classDescStack.size() > stackMark &&
(currentClassDesc = (ObjectStreamClass)classDescStack.pop()) != null);
} finally {
classDescStack.setSize(stackMark);
}
}
}
示例14: outputObject
import java.io.Externalizable; //导入方法依赖的package包/类
private void outputObject(final Object obj) throws IOException{
currentObject = obj;
Class currclass = obj.getClass();
/* Get the Class descriptor for this class,
* Throw a NotSerializableException if there is none.
*/
currentClassDesc = ObjectStreamClass.lookup(currclass);
if (currentClassDesc == null) {
// XXX I18N, Logging needed.
throw new NotSerializableException(currclass.getName());
}
/* If the object is externalizable,
* call writeExternal.
* else do Serializable processing.
*/
if (currentClassDesc.isExternalizable()) {
// Write format version
orbStream.write_octet(streamFormatVersion);
Externalizable ext = (Externalizable)obj;
ext.writeExternal(this);
} else {
/* The object's classes should be processed from supertype to subtype
* Push all the clases of the current object onto a stack.
* Remember the stack pointer where this set of classes is being pushed.
*/
if (currentClassDesc.forClass().getName().equals("java.lang.String")) {
this.writeUTF((String)obj);
return;
}
int stackMark = classDescStack.size();
try {
ObjectStreamClass next;
while ((next = currentClassDesc.getSuperclass()) != null) {
classDescStack.push(currentClassDesc);
currentClassDesc = next;
}
/*
* For currentClassDesc and all the pushed class descriptors
* If the class is writing its own data
* set blockData = true; call the class writeObject method
* If not
* invoke either the defaultWriteObject method.
*/
do {
WriteObjectState oldState = writeObjectState;
try {
setState(NOT_IN_WRITE_OBJECT);
if (currentClassDesc.hasWriteObject()) {
invokeObjectWriter(currentClassDesc, obj );
} else {
defaultWriteObjectDelegate();
}
} finally {
setState(oldState);
}
} while (classDescStack.size() > stackMark &&
(currentClassDesc = (ObjectStreamClass)classDescStack.pop()) != null);
} finally {
classDescStack.setSize(stackMark);
}
}
}
示例15: toStream
import java.io.Externalizable; //导入方法依赖的package包/类
public static void toStream(@Nonnull final Externalizable obj, @Nonnull final OutputStream out)
throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(out);
obj.writeExternal(oos);
oos.flush();
}