java.io.ObjectStreamConstants Java Examples

The following examples show how to use java.io.ObjectStreamConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: BaRMIe   Author: NickstaDB   File: RMIReplyDataParser.java    License: MIT License 6 votes vote down vote up
/*******************
 * Handle a string element.
 * 
 * @param obj The RMIObject to populate with class names.
 ******************/
private void handleStringElement(LinkedList<Byte> dataStack) throws BaRMIeInvalidReplyDataPacketException {
	//Handle a string based on the type
	switch(dataStack.pop()) {
		//Standard string
		case ObjectStreamConstants.TC_STRING:
			this.extractUtf8(dataStack);
			break;
			
		//Long string
		case ObjectStreamConstants.TC_LONGSTRING:
			this.extractLongUtf8(dataStack);
			break;
			
		//References
		case ObjectStreamConstants.TC_REFERENCE:
			this.extractInt(dataStack);
			break;
			
		//Invalid string type
		default:
			throw new BaRMIeInvalidReplyDataPacketException("Invalid string element type.");
	}
}
 
Example #2
Source Project: joshua   Author: apache   File: BinaryIn.java    License: Apache License 2.0 6 votes vote down vote up
public E readObject() throws ClassNotFoundException, IOException {

    int b = peek();
    if (b == ObjectStreamConstants.TC_NULL) {
      return null;
    } else {
      E obj;
      try {
        obj = type.newInstance();
        obj.readExternal(this);
        return obj;
      } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
      }
    }
  }
 
Example #3
Source Project: buck   Author: facebook   File: FatJar.java    License: Apache License 2.0 6 votes vote down vote up
/** Serialize this instance as binary to {@code outputStream}. */
public void store(OutputStream outputStream) throws IOException {
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
  try {
    // Explicitly specify a protocol version, just in case the default protocol gets updated with
    // a new version of Java. We need to ensure the serialized data can be read by older versions
    // of Java, as the fat jar stub, which references this class, is compiled against an older
    // version of Java for compatibility purposes, unlike the main Buck jar, which also references
    // this class.
    objectOutputStream.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);

    objectOutputStream.writeObject(this);
  } finally {
    objectOutputStream.close();
  }
}
 
Example #4
Source Project: jdk1.8-source-analysis   Author: raysonfang   File: MarshalledObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #5
Source Project: dragonwell8_jdk   Author: alibaba   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #6
Source Project: dragonwell8_jdk   Author: alibaba   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    Field field = serClass.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #7
Source Project: netty-4.1.22   Author: tianheframe   File: CompatibleMarshallingDecoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void decodeLast(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    switch (buffer.readableBytes()) {
    case 0:
        return;
    case 1:
        // Ignore the last TC_RESET
        if (buffer.getByte(buffer.readerIndex()) == ObjectStreamConstants.TC_RESET) {
            buffer.skipBytes(1);
            return;
        }
    }

    decode(ctx, buffer, out);
}
 
Example #8
Source Project: TencentKona-8   Author: Tencent   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #9
Source Project: TencentKona-8   Author: Tencent   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    Field field = serClass.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #10
Source Project: jdk8u60   Author: chenghanpeng   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #11
Source Project: jdk8u60   Author: chenghanpeng   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    Field field = serClass.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #12
Source Project: JDKSourceCode1.8   Author: wupeixuan   File: MarshalledObject.java    License: MIT License 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #13
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #14
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    Field field = serClass.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #15
Source Project: BaRMIe   Author: NickstaDB   File: RMIReplyDataParser.java    License: MIT License 5 votes vote down vote up
/*******************
 * Handle a classDesc element.
 * 
 * @param obj The RMIObject to populate with class names.
 * @param dataStack The remaining data in the ReplyData packet.
 ******************/
private void handleClassDesc(RMIObject obj, LinkedList<Byte> dataStack) throws BaRMIeInvalidReplyDataPacketException {
	String className;
	
	//Delegate depending on the type of classDesc
	switch(dataStack.pop()) {
		//ClassDesc
		case ObjectStreamConstants.TC_CLASSDESC:
			//Read the class name
			className = this.extractUtf8(dataStack);
			
			//Skip over the serialVersionUID
			this.extractLong(dataStack);
			
			//Handle the classDescInfo element, pass the class name in as there may be annotations for the class in there
			this.handleClassDescInfo(obj, dataStack, className);
			break;
			
		//ProxyClassDesc
		case ObjectStreamConstants.TC_PROXYCLASSDESC:
			//Handle the proxyClassDescInfo element
			this.handleProxyClassDescInfo(obj, dataStack);
			break;
			
		//Null - e.g. when the super class is null
		case ObjectStreamConstants.TC_NULL:
			break;
			
		//Unknown classDesc type
		default:
			throw new BaRMIeInvalidReplyDataPacketException("Unknown classDesc element type.");
	}
}
 
Example #16
Source Project: BaRMIe   Author: NickstaDB   File: RMIReplyDataParser.java    License: MIT License 5 votes vote down vote up
/*******************
 * Handle a classAnnotation element and return any string annotation
 * elements in the classAnnotation.
 * 
 * @param obj The RMIObject to populate with class names.
 * @param dataStack The remaining data in the ReplyData packet.
 * @return An ArrayList of strings representing any string annotations extracted from the stream.
 ******************/
private ArrayList<String> handleClassAnnotation(RMIObject obj, LinkedList<Byte> dataStack) throws BaRMIeInvalidReplyDataPacketException {
	ArrayList<String> stringAnnotations;
	byte b;
	
	//Create the arraylist
	stringAnnotations = new ArrayList<String>();
	
	//Read elements from the stream until a TC_ENDBLOCKDATA element is read
	while((b = dataStack.pop()) != ObjectStreamConstants.TC_ENDBLOCKDATA) {
		//Handle the annotation
		switch(b) {
			//Read string annotations into an array list to return
			case ObjectStreamConstants.TC_STRING:
				stringAnnotations.add(this.extractUtf8(dataStack));
				break;
				
			//Skip over reference annotations
			case ObjectStreamConstants.TC_REFERENCE:
				//Read past the reference handle
				this.extractInt(dataStack);
				break;
				
			//Ignore null annotations...
			case ObjectStreamConstants.TC_NULL:
				break;
				
			//Unknown annotation type
			default:
				throw new BaRMIeInvalidReplyDataPacketException("Unknown classAnnotation element type (0x" + String.format("%02x", b) + ").");
		}
	}
	
	//Return the string annotations
	return stringAnnotations;
}
 
Example #17
Source Project: BaRMIe   Author: NickstaDB   File: RMIReplyDataParser.java    License: MIT License 5 votes vote down vote up
/*******************
 * Handle an objectAnnotation element, extracting the object endpoint
 * details if found.
 * 
 * @param obj The RMIObject to populate with class names.
 * @param dataStack The remaining data in the ReplyData packet.
 ******************/
private void handleObjectAnnotation(RMIObject obj, LinkedList<Byte> dataStack) throws BaRMIeInvalidReplyDataPacketException {
	byte b;
	
	//Read elements from the stream until a TC_ENDBLOCKDATA element is read
	while((b = dataStack.pop()) != ObjectStreamConstants.TC_ENDBLOCKDATA) {
		//Handle the annotation
		switch(b) {
			//Look for object endpoint details in block data elements
			case ObjectStreamConstants.TC_BLOCKDATA:
				//Push the block type back on to the stack and extract endpoint details if found
				dataStack.push(ObjectStreamConstants.TC_BLOCKDATA);
				this.extractObjectEndpointFromBlockData(obj, dataStack);
				break;
				
			//Skip over object annotations
			case ObjectStreamConstants.TC_OBJECT:
				this.handleNewObjectElement(obj, dataStack);
				break;
				
			//Ignore null annotations...
			case ObjectStreamConstants.TC_NULL:
				break;
				
			//Unknown annotation type
			default:
				throw new BaRMIeInvalidReplyDataPacketException("Unknown classAnnotation element type (0x" + String.format("%02x", b) + ").");
		}
	}
}
 
Example #18
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #19
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    Field field = serClass.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #20
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: TCKChronoLocalDateSerialization.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void test_hijrahSerialization_format() throws Exception {
    HijrahChronology chrono = HijrahChronology.INSTANCE;
    HijrahDate date = HijrahDate.of(1433, 10, 29);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Expect the type of the HijrahDate in the stream
    byte[] hijrahDateBytes = new byte[] {HIJRAH_DATE_TYPE};

    // Literal reference to Hijrah-Umalqura Chronology
    byte[] hijrahChronoBytes = new byte[] {
        115, 113, 0, 126, 0, 0,                        /* p w \u0001 \u0006 s q \u0000 ~ \u0000 \u0000 */
        119, 18, 1, 0, 15, 72, 105, 106, 114, 97,      /* w \u0012 \u0001 \u0000 \u000f H i j r a */
        104, 45, 117, 109, 97, 108, 113, 117, 114, 97, /* h - u m a l q u r a */
        120,                                           /*  \u001d x */
    };

    // Build the sequence that represents the data in the stream
    baos = new ByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(baos) ) {
        dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
        dos.writeByte(6);   // 6 bytes follow
        dos.writeInt(date.get(YEAR));
        dos.writeByte(date.get(MONTH_OF_YEAR));
        dos.writeByte(date.get(DAY_OF_MONTH));
        dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
    }
    byte[] dateBytes = baos.toByteArray();

    assertSerializedBySer(date, hijrahDateBytes, hijrahChronoBytes, dateBytes);
}
 
Example #21
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #22
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    long serVer = getSUID(serClass);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #23
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TCKChronoLocalDateSerialization.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void test_hijrahSerialization_format() throws Exception {
    HijrahChronology chrono = HijrahChronology.INSTANCE;
    HijrahDate date = HijrahDate.of(1433, 10, 29);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Expect the type of the HijrahDate in the stream
    byte[] hijrahDateBytes = new byte[] {HIJRAH_DATE_TYPE};

    // Literal reference to Hijrah-Umalqura Chronology
    byte[] hijrahChronoBytes = new byte[] {
        115, 113, 0, 126, 0, 0,                        /* p w \u0001 \u0006 s q \u0000 ~ \u0000 \u0000 */
        119, 18, 1, 0, 15, 72, 105, 106, 114, 97,      /* w \u0012 \u0001 \u0000 \u000f H i j r a */
        104, 45, 117, 109, 97, 108, 113, 117, 114, 97, /* h - u m a l q u r a */
        120,                                           /*  \u001d x */
    };

    // Build the sequence that represents the data in the stream
    baos = new ByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(baos) ) {
        dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
        dos.writeByte(6);   // 6 bytes follow
        dos.writeInt(date.get(YEAR));
        dos.writeByte(date.get(MONTH_OF_YEAR));
        dos.writeByte(date.get(DAY_OF_MONTH));
        dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
    }
    byte[] dateBytes = baos.toByteArray();

    assertSerializedBySer(date, hijrahDateBytes, hijrahChronoBytes, dateBytes);
}
 
Example #24
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #25
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    Field field = serClass.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #26
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: TCKChronoLocalDateSerialization.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void test_hijrahSerialization_format() throws Exception {
    HijrahChronology chrono = HijrahChronology.INSTANCE;
    HijrahDate date = HijrahDate.of(1433, 10, 29);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Expect the type of the HijrahDate in the stream
    byte[] hijrahDateBytes = new byte[] {HIJRAH_DATE_TYPE};

    // Literal reference to Hijrah-Umalqura Chronology
    byte[] hijrahChronoBytes = new byte[] {
        115, 113, 0, 126, 0, 0,                        /* p w \u0001 \u0006 s q \u0000 ~ \u0000 \u0000 */
        119, 18, 1, 0, 15, 72, 105, 106, 114, 97,      /* w \u0012 \u0001 \u0000 \u000f H i j r a */
        104, 45, 117, 109, 97, 108, 113, 117, 114, 97, /* h - u m a l q u r a */
        120,                                           /*  \u001d x */
    };

    // Build the sequence that represents the data in the stream
    baos = new ByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(baos) ) {
        dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
        dos.writeByte(6);   // 6 bytes follow
        dos.writeInt(date.get(YEAR));
        dos.writeByte(date.get(MONTH_OF_YEAR));
        dos.writeByte(date.get(DAY_OF_MONTH));
        dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
    }
    byte[] dateBytes = baos.toByteArray();

    assertSerializedBySer(date, hijrahDateBytes, hijrahChronoBytes, dateBytes);
}
 
Example #27
Source Project: Java8CN   Author: Java8-CNAPI-Team   File: MarshalledObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #28
Source Project: hottub   Author: dsrg-uoft   File: MarshalledObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>MarshalledObjectOutputStream</code> whose
 * non-location bytes will be written to <code>objOut</code> and whose
 * location annotations (if any) will be written to
 * <code>locOut</code>.
 */
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
    throws IOException
{
    super(objOut);
    this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
    this.locOut = new ObjectOutputStream(locOut);
    hadAnnotations = false;
}
 
Example #29
Source Project: hottub   Author: dsrg-uoft   File: AbstractTCKTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the class cannot be deserialized from a handcoded stream.
 * Fail if the deserialization does <em>not</em> throw an Exception.
 * @param serClass the class to embed in the handcoded stream
 * @throws Exception if an unexpected condition occurs
 */
protected static void assertNotSerializable(Class<?> serClass) throws Exception {
    Field field = serClass.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(baos)) {
        out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        out.writeShort(ObjectStreamConstants.STREAM_VERSION);
        out.writeByte(ObjectStreamConstants.TC_OBJECT);
        out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        out.writeUTF(serClass.getName());
        out.writeLong(serVer);
        out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
        out.writeShort(0);  // number of fields
        out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
    }

    byte[] bytes = baos.toByteArray();

    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
    } catch (Exception ioe) {
        // Expected exception
        return;
    }
    fail("Class should not be deserializable " + serClass.getName());
}
 
Example #30
Source Project: hottub   Author: dsrg-uoft   File: TCKChronoLocalDateSerialization.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void test_hijrahSerialization_format() throws Exception {
    HijrahChronology chrono = HijrahChronology.INSTANCE;
    HijrahDate date = HijrahDate.of(1433, 10, 29);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Expect the type of the HijrahDate in the stream
    byte[] hijrahDateBytes = new byte[] {HIJRAH_DATE_TYPE};

    // Literal reference to Hijrah-Umalqura Chronology
    byte[] hijrahChronoBytes = new byte[] {
        115, 113, 0, 126, 0, 0,                        /* p w \u0001 \u0006 s q \u0000 ~ \u0000 \u0000 */
        119, 18, 1, 0, 15, 72, 105, 106, 114, 97,      /* w \u0012 \u0001 \u0000 \u000f H i j r a */
        104, 45, 117, 109, 97, 108, 113, 117, 114, 97, /* h - u m a l q u r a */
        120,                                           /*  \u001d x */
    };

    // Build the sequence that represents the data in the stream
    baos = new ByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(baos) ) {
        dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
        dos.writeByte(6);   // 6 bytes follow
        dos.writeInt(date.get(YEAR));
        dos.writeByte(date.get(MONTH_OF_YEAR));
        dos.writeByte(date.get(DAY_OF_MONTH));
        dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
    }
    byte[] dateBytes = baos.toByteArray();

    assertSerializedBySer(date, hijrahDateBytes, hijrahChronoBytes, dateBytes);
}