/// <summary> /// Decode special chars. /// </summary> private static void DecodeEscaped() { FOA.Entity entity = new FOA.Entity(); FOA.Decoder decoder = new FOA.Decoder(); Console.WriteLine("Escape enabled:"); decoder.SetBuffer(Encoding.UTF8.GetBytes("a%28b%5Bc%5Dd%29e%3Df\n")); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } decoder.SetBuffer(Encoding.UTF8.GetBytes("name = a%28b%5Bc%5Dd%29e%3Df\n")); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } Console.WriteLine("Escape disabled:"); decoder.SetOption(FOA.Option.EnableEscape, false); decoder.SetBuffer(Encoding.UTF8.GetBytes("name = a%28b%5Bc%5Dd%29e%3Df\n")); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } }
/// <summary> /// Decode a *lot* of objects. This code might throw an OutOfMemoryException (catched). /// </summary> private static void DecodeMultiObject() { long num = 20000000, i; string obj = "(adam\n34\n)\n"; try { Console.Write("Creating object array... "); StringBuilder sb = new StringBuilder("[\n"); for (i = 0; i < num; ++i) { sb.Append(obj); } sb.Append("]\n"); Console.WriteLine("done"); Console.Write("Contructing decoder object... "); FOA.Decoder decoder = new FOA.Decoder(sb.ToString()); Console.WriteLine("done"); FOA.Entity entity = new FOA.Entity(); Console.Write("Decoding {0} objects... ", num); while (decoder.Read(ref entity)) { } Console.WriteLine("done"); } catch (OutOfMemoryException exception) { Console.Error.WriteLine(exception.Message); } }
/// <summary> /// Decode and array of objects. /// </summary> private static void DecodeObjectArray() { byte[] buffer = Encoding.UTF8.GetBytes(String.Format("arr = [\nobj1 = (\nname = adam\nage = 24\n)\nobj2 = (\nname = adam\nage = 24\n)\n]\n")); FOA.Decoder decoder = new FOA.Decoder(buffer); FOA.Entity entity = new FOA.Entity(); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } }
/// <summary> /// Decode a simple, anonymous object. /// </summary> private static void DecodeSimpleObject() { byte[] buffer = Encoding.UTF8.GetBytes(String.Format("(\nadam\n24\n)\n")); FOA.Decoder decoder = new FOA.Decoder(buffer); FOA.Entity entity = new FOA.Entity(); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } }
/// <summary> /// Decode a FOA object from a byte array. /// </summary> private static void DecodeBufferObject() { byte[] buffer = Encoding.UTF8.GetBytes("obj = (\nname = adam\nage = 24\n)\n"); FOA.Decoder decoder = new FOA.Decoder(buffer); FOA.Entity entity = new FOA.Entity(); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } }
/// <summary> /// Decode FOA entities from a file stream. /// </summary> private static void DecodeFileStream() { using (FileStream stream = new FileStream("..\\..\\Data.txt", FileMode.Open)) { FOA.Decoder decoder = new FOA.Decoder(stream); FOA.Entity entity = new FOA.Entity(); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } } }
/// <summary> /// Decode a FOA object from a memory stream. /// </summary> private static void DecodeMemoryStream() { byte[] buffer = Encoding.UTF8.GetBytes("obj = (\nname = adam\nage = 24\n)\n"); MemoryStream stream = new MemoryStream(buffer); FOA.Decoder decoder = new FOA.Decoder(stream); FOA.Entity entity = new FOA.Entity(); while (decoder.Read(ref entity)) { PrintEntity(ref entity); } }