Esempio n. 1
0
        /// <summary>
        /// Reads an automaton from a byte stream.
        /// </summary>
        /// <param name="stream">The stream to read.</param>
        internal CFSA2(Stream stream)
        {
            using (DataInputStream input = new DataInputStream(stream, true))
            {
                // Read flags.
                ushort flagBits = (ushort)input.ReadInt16();
                flags = 0;
                flags = (FSAFlags)flagBits;

                if (flagBits != (ushort)flags)
                {
                    throw new IOException($"Unrecognized flags: 0x{((int)flagBits).ToHexString()}");
                }

                this.hasNumbers = (flags & FSAFlags.Numbers) != 0;

                /*
                 * Read mapping dictionary.
                 */
                int labelMappingSize = input.ReadByte() & 0xff;

                LabelMapping = new byte[labelMappingSize];

                input.ReadFully(LabelMapping);

                /*
                 * Read arcs' data.
                 */
                Arcs = ReadRemaining(input);
            }
        }
Esempio n. 2
0
        private static void ProcessPostRequest(HttpRequest request, HttpResponse response, HttpSessionState session)
        {
            response.ContentType = "application/octet-stream";

            var memoryStream = new MemoryStream();
            var output       = new DataOutputStream(memoryStream);

            try
            {
                var input   = new DataInputStream(request.InputStream);
                var version = request.Headers["version"];
                if (version != null)
                {
                    if (!HttpProcessor.PROTOCOL_VERSION.Equals(version, StringComparison.Ordinal))
                    {
                        throw new IOException(String.Format(
                                                  CultureInfo.CurrentCulture,
                                                  Resources.IO_InvalidProtocolVersion,
                                                  version,
                                                  HttpProcessor.PROTOCOL_VERSION));
                    }
                }

                if (input.ReadInt16() == HttpProcessor.INVOCATION_CODE)
                {
                    InvokeMethod(session, input.ReadInt32(), input, output);
                }
                input.Close();
            }
            catch (Exception e)
            {
                if (output == null)
                {
                    output = new DataOutputStream(response.OutputStream);
                }

                output.WriteInt16(HttpProcessor.RESULT_EXCEPTION);
                Debug.WriteLine(e.StackTrace);
                output.WriteString(e.ToString());
            }

            response.SetContentLength(memoryStream.Length);

            try
            {
                var data = new Byte[memoryStream.Length];
                memoryStream.Seek(0, SeekOrigin.Begin);
                memoryStream.Read(data, 0, data.Length);
                response.OutputStream.Write(data, 0, data.Length);
            }
            finally
            {
                if (output != null)
                {
                    output.Close();
                }
                response.OutputStream.Close();
            }
        }