/// <summary> /// Reads an AMF message from an input stream. /// </summary> /// <param name="input">The input stream</param> /// <returns>The AMF message that was read, or null on end of stream</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="input"/> is null</exception> /// <exception cref="AMFException">Thrown if an exception occurred while writing the message</exception> public static AMFMessage ReadAMFMessage(AMFDataInput input) { if (input == null) { throw new ArgumentNullException("input"); } try { // Check for end of stream. // If we catch an EndOfStreamException here, we gracefully unwind and // return null to the caller. If it happens later on then it's an error // because it means the message is incomplete. byte firstByte; try { firstByte = input.ReadByte(); } catch (EndOfStreamException) { // Return gracefully. return(null); } // Okay, there's data. From now on, if we see EndOfStreamException it's an error! return(UncheckedReadAMFMessage(input, firstByte)); } catch (Exception ex) { throw new AMFException("An exception occurred while reading an AMF message from the stream.", ex); } }
/// <summary> /// Reads an AMF message from an input stream. /// </summary> /// <param name="input">The input stream</param> /// <returns>The AMF message that was read, or null on end of stream</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="input"/> is null</exception> /// <exception cref="AMFException">Thrown if an exception occurred while writing the message</exception> public static AMFMessage ReadAMFMessage(AMFDataInput input) { if (input == null) throw new ArgumentNullException("input"); try { // Check for end of stream. // If we catch an EndOfStreamException here, we gracefully unwind and // return null to the caller. If it happens later on then it's an error // because it means the message is incomplete. byte firstByte; try { firstByte = input.ReadByte(); } catch (EndOfStreamException) { // Return gracefully. return null; } // Okay, there's data. From now on, if we see EndOfStreamException it's an error! return UncheckedReadAMFMessage(input, firstByte); } catch (Exception ex) { throw new AMFException("An exception occurred while reading an AMF message from the stream.", ex); } }
/// <summary> /// Reads an AMF message from an input stream and bubbles up exceptions. /// </summary> /// <param name="input">The input stream</param> /// <param name="firstByte">The first byte of the message</param> /// <returns>The AMF message that was read</returns> private static AMFMessage UncheckedReadAMFMessage(AMFDataInput input, byte firstByte) { byte secondVersionByte = input.ReadByte(); ushort version = (ushort) ((firstByte << 8) | secondVersionByte); int headerCount = input.ReadUnsignedShort(); AMFHeader[] headers = new AMFHeader[headerCount]; for (int i = 0; i < headerCount; i++) headers[i] = ReadAMFHeader(input); int bodyCount = input.ReadUnsignedShort(); AMFBody[] bodies = new AMFBody[bodyCount]; for (int i = 0; i < bodyCount; i++) bodies[i] = ReadAMFBody(input); return new AMFMessage(version, headers, bodies); }
/// <summary> /// Reads an AMF message from an input stream and bubbles up exceptions. /// </summary> /// <param name="input">The input stream</param> /// <param name="firstByte">The first byte of the message</param> /// <returns>The AMF message that was read</returns> private static AMFMessage UncheckedReadAMFMessage(AMFDataInput input, byte firstByte) { byte secondVersionByte = input.ReadByte(); ushort version = (ushort)((firstByte << 8) | secondVersionByte); int headerCount = input.ReadUnsignedShort(); AMFHeader[] headers = new AMFHeader[headerCount]; for (int i = 0; i < headerCount; i++) { headers[i] = ReadAMFHeader(input); } int bodyCount = input.ReadUnsignedShort(); AMFBody[] bodies = new AMFBody[bodyCount]; for (int i = 0; i < bodyCount; i++) { bodies[i] = ReadAMFBody(input); } return(new AMFMessage(version, headers, bodies)); }
public IASValue ReadObject() { if (!isAMF3) { // Decide what to do based on the type code. AMF0ObjectTypeCode typeCode = (AMF0ObjectTypeCode)input.ReadByte(); switch (typeCode) { case AMF0ObjectTypeCode.AMF3Data: isAMF3 = true; return(amf3ObjectReader.ReadObject()); case AMF0ObjectTypeCode.Array: return(ReadArray()); case AMF0ObjectTypeCode.Boolean: return(ReadBoolean()); case AMF0ObjectTypeCode.Date: return(ReadDate()); case AMF0ObjectTypeCode.LongString: return(ReadLongString()); case AMF0ObjectTypeCode.MixedArray: return(ReadMixedArray()); case AMF0ObjectTypeCode.Null: return(null); case AMF0ObjectTypeCode.Number: return(ReadNumber()); case AMF0ObjectTypeCode.Object: return(ReadObjectValue()); case AMF0ObjectTypeCode.Reference: return(ReadReference()); case AMF0ObjectTypeCode.ShortString: return(ReadShortString()); case AMF0ObjectTypeCode.TypedObject: return(ReadTypedObject()); case AMF0ObjectTypeCode.Undefined: return(ASUndefined.Value); case AMF0ObjectTypeCode.Unsupported: return(ASUnsupported.Value); case AMF0ObjectTypeCode.Xml: return(ReadXml()); default: throw new AMFException(String.Format(CultureInfo.CurrentCulture, ExceptionPrefix + "Encountered unexpected or unsupported token '{0}'.", typeCode)); } } // Delegate to AMF3 object reader. return(amf3ObjectReader.ReadObject()); }