下面列出了java.io.ObjectOutputStream#writeByte ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void writeObject(ObjectOutputStream out) throws IOException {
if (isInBytes()) {
out.writeByte(serialization); // Write serialization type
out.writeInt(bytes.length); // Write byte stream size
out.write(bytes); // Write the byte stream
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream javaos = new ObjectOutputStream(bos);
try {
javaos.writeObject(object);
} finally {
try {
javaos.close();
} catch (IOException ioe) {
//do nothing
}
}
out.writeByte(SERIALIZED_BY_JAVA); // Write serialization type
out.writeInt(bos.size()); // Write byte stream size
out.write(bos.toByteArray()); // Write the byte stream
}
}
/**
* Run benchmark for given number of batches, with given number of cycles
* for each batch.
*/
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
StreamBuffer sbuf, int nbatches, int ncycles)
throws Exception
{
for (int i = 0; i < nbatches; i++) {
sbuf.reset();
for (int j = 0; j < ncycles; j++) {
oout.writeByte(0);
}
oout.flush();
for (int j = 0; j < ncycles; j++) {
oin.readByte();
}
}
}
/**
* Writes the contents of this CacheHeader to the specified OutputStream.
*/
public boolean writeHeader(OutputStream os) {
try {
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeByte(CACHE_VERSION);
oos.writeUTF(key);
oos.writeUTF(etag == null ? "" : etag);
oos.writeLong(serverDate);
oos.writeLong(ttl);
oos.writeLong(softTtl);
writeStringStringMap(responseHeaders, oos);
oos.flush();
return true;
} catch (IOException e) {
VolleyLog.d("%s", e.toString());
return false;
}
}
/**
* Run benchmark for given number of batches, with given number of cycles
* for each batch.
*/
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
StreamBuffer sbuf, int nbatches, int ncycles)
throws Exception
{
for (int i = 0; i < nbatches; i++) {
sbuf.reset();
for (int j = 0; j < ncycles; j++) {
oout.writeByte(0);
}
oout.flush();
for (int j = 0; j < ncycles; j++) {
oin.readByte();
}
}
}
/**
* Run benchmark for given number of batches, with given number of cycles
* for each batch.
*/
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
StreamBuffer sbuf, int nbatches, int ncycles)
throws Exception
{
for (int i = 0; i < nbatches; i++) {
sbuf.reset();
for (int j = 0; j < ncycles; j++) {
oout.writeByte(0);
}
oout.flush();
for (int j = 0; j < ncycles; j++) {
oin.readByte();
}
}
}
/**
* Writes an array of parameter types to the stream.
*
* Requires special handling because primitive types cannot be
* found upon deserialization by the default Java implementation.
*/
private static void writeParameters(ObjectOutputStream out, Class<?>[] parms)
throws IOException
{
out.writeShort(parms.length);
outer:
for (int i=0; i < parms.length; i++) {
Class<?> parm = parms[i];
boolean primitive = parm.isPrimitive();
out.writeBoolean(primitive);
if (!primitive) {
out.writeObject(parm);
continue;
}
for (int j=0; j < primitives.length; j++) {
if (parm.equals(primitives[j])) {
out.writeByte(j);
continue outer;
}
}
throw new IllegalArgumentException("Primitive " + parm +
" not found");
}
}
/**
* Run benchmark for given number of batches, with given number of cycles
* for each batch.
*/
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
StreamBuffer sbuf, int nbatches, int ncycles)
throws Exception
{
for (int i = 0; i < nbatches; i++) {
sbuf.reset();
for (int j = 0; j < ncycles; j++) {
oout.writeByte(0);
}
oout.flush();
for (int j = 0; j < ncycles; j++) {
oin.readByte();
}
}
}
/**
* Run benchmark for given number of batches, with given number of cycles
* for each batch.
*/
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
StreamBuffer sbuf, int nbatches, int ncycles)
throws Exception
{
for (int i = 0; i < nbatches; i++) {
sbuf.reset();
for (int j = 0; j < ncycles; j++) {
oout.writeByte(0);
}
oout.flush();
for (int j = 0; j < ncycles; j++) {
oin.readByte();
}
}
}
/**
* Run benchmark for given number of batches, with given number of cycles
* for each batch.
*/
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
StreamBuffer sbuf, int nbatches, int ncycles)
throws Exception
{
for (int i = 0; i < nbatches; i++) {
sbuf.reset();
for (int j = 0; j < ncycles; j++) {
oout.writeByte(0);
}
oout.flush();
for (int j = 0; j < ncycles; j++) {
oin.readByte();
}
}
}
/**
* Run benchmark for given number of batches, with given number of cycles
* for each batch.
*/
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
StreamBuffer sbuf, int nbatches, int ncycles)
throws Exception
{
for (int i = 0; i < nbatches; i++) {
sbuf.reset();
for (int j = 0; j < ncycles; j++) {
oout.writeByte(0);
}
oout.flush();
for (int j = 0; j < ncycles; j++) {
oin.readByte();
}
}
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeByte(MAJOR);
out.writeByte(MINOR);
if (parameters == null) {
out.writeInt(-1);
} else {
out.writeInt(parameters.length);
for (Object element : parameters) {
out.writeObject((element == null) ? null : element.toString());
}
}
}
public static byte[] encode(MapResponse response) throws IOException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOut);
output.writeByte(response.responseType().ordinal());
response.writeTo(output);
output.close();
return byteOut.toByteArray();
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeBoolean(z);
out.writeByte(b);
out.writeChar(c);
out.writeShort(s);
out.writeInt(i);
out.writeFloat(f);
out.writeLong(j);
out.writeDouble(d);
out.writeObject(str);
out.writeObject(parent);
out.writeObject(left);
out.writeObject(right);
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeBoolean(z);
out.writeByte(b);
out.writeChar(c);
out.writeShort(s);
out.writeInt(i);
out.writeFloat(f);
out.writeLong(j);
out.writeDouble(d);
out.writeObject(str);
out.writeObject(parent);
out.writeObject(left);
out.writeObject(right);
}
public static byte[] encode(MapOperation operation) throws IOException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(byteOut);
output.writeByte(operation.operationType().ordinal());
operation.writeTo(output);
output.close();
return byteOut.toByteArray();
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeBoolean(z);
out.writeByte(b);
out.writeChar(c);
out.writeShort(s);
out.writeInt(i);
out.writeFloat(f);
out.writeLong(j);
out.writeDouble(d);
out.writeObject(str);
out.writeObject(parent);
out.writeObject(left);
out.writeObject(right);
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeBoolean(z);
out.writeByte(b);
out.writeChar(c);
out.writeShort(s);
out.writeInt(i);
out.writeFloat(f);
out.writeLong(j);
out.writeDouble(d);
out.writeObject(str);
out.writeObject(parent);
out.writeObject(left);
out.writeObject(right);
}
private void doCall ( DataInputStream in, DataOutputStream out, Object payload ) throws Exception {
ObjectInputStream ois = new ObjectInputStream(in) {
@Override
protected Class<?> resolveClass ( ObjectStreamClass desc ) throws IOException, ClassNotFoundException {
if ( "[Ljava.rmi.server.ObjID;".equals(desc.getName())) {
return ObjID[].class;
} else if ("java.rmi.server.ObjID".equals(desc.getName())) {
return ObjID.class;
} else if ( "java.rmi.server.UID".equals(desc.getName())) {
return UID.class;
}
throw new IOException("Not allowed to read object");
}
};
ObjID read;
try {
read = ObjID.read(ois);
}
catch ( java.io.IOException e ) {
throw new MarshalException("unable to read objID", e);
}
if ( read.hashCode() == 2 ) {
ois.readInt(); // method
ois.readLong(); // hash
System.err.println("Is DGC call for " + Arrays.toString((ObjID[])ois.readObject()));
}
System.err.println("Sending return with payload for obj " + read);
out.writeByte(TransportConstants.Return);// transport op
ObjectOutputStream oos = new JRMPClient.MarshalOutputStream(out, this.classpathUrl);
oos.writeByte(TransportConstants.ExceptionalReturn);
new UID().write(oos);
BadAttributeValueExpException ex = new BadAttributeValueExpException(null);
Reflections.setFieldValue(ex, "val", payload);
oos.writeObject(ex);
oos.flush();
out.flush();
this.hadConnection = true;
synchronized ( this.waitLock ) {
this.waitLock.notifyAll();
}
}
/**
* A helper method that serializes the element object.
*
* @param stream
* the stream to which the element should be serialized.
* @throws IOException
* if an IO error occured or a property was not serializable.
*/
private void writeObject( final ObjectOutputStream stream ) throws IOException {
stream.defaultWriteObject();
final ReportAttributeMap attributes = this.attributes;
stream.writeLong( attributes.getChangeTracker() );
final String[] nameSpaces = attributes.getNameSpaces();
stream.writeObject( nameSpaces );
for ( int i = 0; i < nameSpaces.length; i++ ) {
final String nameSpace = nameSpaces[i];
final String[] names = attributes.getNames( nameSpace );
stream.writeObject( names );
for ( int j = 0; j < names.length; j++ ) {
final String name = names[j];
final Object attribute = attributes.getAttribute( nameSpace, name );
final AttributeMetaData data = getMetaData().getAttributeDescription( nameSpace, name );
if ( data != null ) {
if ( data.isTransient() ) {
stream.writeByte( 1 );
continue;
}
if ( attribute instanceof ResourceKey ) {
final ResourceKey key = (ResourceKey) attribute;
final ResourceKey parent = key.getParent();
if ( AttributeNames.Core.NAMESPACE.equals( nameSpace )
&& ( AttributeNames.Core.CONTENT_BASE.equals( name ) || AttributeNames.Core.SOURCE.equals( name ) ) ) {
if ( parent != null ) {
// unwrap the content base attribute. After deserialization, the report assumes the bundle-location
// as content base, as the bundle will be gone.
if ( isKeySerializable( parent ) ) {
stream.writeByte( 0 );
SerializerHelper.getInstance().writeObject( parent, stream );
} else {
stream.writeByte( 1 );
}
} else {
// great, the report was never part of a bundle. That makes life easier and the key should be
// safely serializable too.
if ( isKeySerializable( key ) ) {
stream.writeByte( 0 );
SerializerHelper.getInstance().writeObject( key, stream );
} else {
stream.writeByte( 1 );
}
}
} else {
if ( "Resource".equals( data.getValueRole() ) || parent != null ) {
stream.writeByte( 0 );
try {
final ResourceKey resourceKey =
ResourceKeyUtils.embedResourceInKey( locateResourceManager(), key, key.getFactoryParameters() );
SerializerHelper.getInstance().writeObject( resourceKey, stream );
} catch ( ResourceException e ) {
throw new IOException( "Failed to convert resource-key into byte-array key: " + e );
}
} else {
stream.writeByte( 0 );
SerializerHelper.getInstance().writeObject( attribute, stream );
}
}
} else if ( SerializerHelper.getInstance().isSerializable( attribute ) ) {
stream.writeByte( 0 );
SerializerHelper.getInstance().writeObject( attribute, stream );
} else {
stream.writeByte( 1 );
}
} else if ( attribute instanceof String ) {
stream.writeByte( 0 );
SerializerHelper.getInstance().writeObject( attribute, stream );
} else {
stream.writeByte( 1 );
}
}
}
}
void writeObject(ObjectOutputStream out) throws IOException {
out.writeInt(mode);
for (int i = 0; i < generalNumbers.length; i++) {
out.writeLong(generalNumbers[i]);
}
if (mode == CommonConstants.MODE_THREADS_SAMPLING) {
out.writeInt(nThreads);
out.writeInt(nThreadStates);
for (int i = 0; i < nThreads; i++) {
out.writeInt(threadIds[i]);
}
for (int i = 0; i < nThreadStates; i++) {
out.writeLong(stateTimestamps[i]);
}
int len = nThreads * nThreadStates;
out.write(threadStates, 0, len);
} else if (mode == CommonConstants.MODE_THREADS_EXACT) {
out.writeInt(exactThreadStates.length);
for (int i = 0; i < exactThreadIds.length; i++) {
out.writeInt(exactThreadIds[i]);
out.writeByte(exactThreadStates[i]);
out.writeLong(exactTimeStamps[i]);
}
}
if (nNewThreads == 0) {
out.writeInt(0);
} else {
out.writeInt(nNewThreads);
for (int i = 0; i < nNewThreads; i++) {
out.writeInt(newThreadIds[i]);
out.writeUTF(newThreadNames[i]);
out.writeUTF(newThreadClassNames[i]);
}
}
out.writeInt(gcStarts.length);
for (int i = 0; i < gcStarts.length; i++) {
out.writeLong(gcStarts[i]);
}
out.writeInt(gcFinishs.length);
for (int i = 0; i < gcFinishs.length; i++) {
out.writeLong(gcFinishs[i]);
}
out.writeInt(serverState);
out.writeInt(serverProgress);
}