diff options
author | adam <adamansky@gmail.com> | 2013-06-10 17:37:41 +0700 |
---|---|---|
committer | adam <adamansky@gmail.com> | 2013-06-10 17:37:41 +0700 |
commit | ce882716c650df875db295bf5b30f25dac1ab266 (patch) | |
tree | baea9f126b7ea95f83512114383b594b5365157e /nejdb/Ejdb.IO | |
parent | 02a4b2dec38af7287ac89deac02266fd864be98e (diff) | |
download | ejdb-ce882716c650df875db295bf5b30f25dac1ab266.tar.gz ejdb-ce882716c650df875db295bf5b30f25dac1ab266.tar.bz2 ejdb-ce882716c650df875db295bf5b30f25dac1ab266.zip |
#24
Diffstat (limited to 'nejdb/Ejdb.IO')
-rw-r--r-- | nejdb/Ejdb.IO/ExtBinaryReader.cs | 54 | ||||
-rw-r--r-- | nejdb/Ejdb.IO/ExtBinaryWriter.cs | 57 |
2 files changed, 111 insertions, 0 deletions
diff --git a/nejdb/Ejdb.IO/ExtBinaryReader.cs b/nejdb/Ejdb.IO/ExtBinaryReader.cs new file mode 100644 index 0000000..dfd8dd2 --- /dev/null +++ b/nejdb/Ejdb.IO/ExtBinaryReader.cs @@ -0,0 +1,54 @@ +// ============================================================================================ +// .NET API for EJDB database library http://ejdb.org +// Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com> +// +// This file is part of EJDB. +// EJDB is free software; you can redistribute it and/or modify it under the terms of +// the GNU Lesser General Public License as published by the Free Software Foundation; either +// version 2.1 of the License or any later version. EJDB is distributed in the hope +// that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +// License for more details. +// You should have received a copy of the GNU Lesser General Public License along with EJDB; +// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, +// Boston, MA 02111-1307 USA. +// ============================================================================================ +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; + +namespace Ejdb.IO { + + public class ExtBinaryReader : BinaryReader { + + public static Encoding DEFAULT_ENCODING = Encoding.UTF8; + + public ExtBinaryReader(Stream input) : this(input, DEFAULT_ENCODING) { + } + + public ExtBinaryReader(Stream input, Encoding encoding) : this(input, encoding, false) { + } + + public ExtBinaryReader(Stream input, bool leaveOpen) : this(input, DEFAULT_ENCODING, leaveOpen) { + } + + public ExtBinaryReader(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen) { + } + + public string ReadCString() { + List<byte> sb = new List<byte>(64); + byte bv; + while ((bv = ReadByte()) != 0x00) { + sb.Add(bv); + } + return Encoding.UTF8.GetString(sb.ToArray()); + } + + public void SkipCString() { + while ((ReadByte()) != 0x00) + ; + } + } +} + diff --git a/nejdb/Ejdb.IO/ExtBinaryWriter.cs b/nejdb/Ejdb.IO/ExtBinaryWriter.cs new file mode 100644 index 0000000..88219b7 --- /dev/null +++ b/nejdb/Ejdb.IO/ExtBinaryWriter.cs @@ -0,0 +1,57 @@ +// ============================================================================================ +// .NET API for EJDB database library http://ejdb.org +// Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com> +// +// This file is part of EJDB. +// EJDB is free software; you can redistribute it and/or modify it under the terms of +// the GNU Lesser General Public License as published by the Free Software Foundation; either +// version 2.1 of the License or any later version. EJDB is distributed in the hope +// that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +// License for more details. +// You should have received a copy of the GNU Lesser General Public License along with EJDB; +// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, +// Boston, MA 02111-1307 USA. +// ============================================================================================ +using System; +using System.IO; +using System.Text; + +namespace Ejdb.IO { + + public class ExtBinaryWriter : BinaryWriter { + + public static Encoding DEFAULT_ENCODING = Encoding.UTF8; + Encoding _encoding; + + public ExtBinaryWriter() { + _encoding = DEFAULT_ENCODING; + } + + public ExtBinaryWriter(Stream output) : this(output, DEFAULT_ENCODING, false) { + } + + public ExtBinaryWriter(Stream output, Encoding encoding, bool leaveopen) : base(output, encoding, leaveopen) { + _encoding = encoding; + } + + public ExtBinaryWriter(Stream output, Encoding encoding) : this(output, encoding, false) { + } + + public ExtBinaryWriter(Stream output, bool leaveopen) : this(output, DEFAULT_ENCODING, leaveopen) { + } + + public void WriteBSONString(string val) { + byte[] buf = _encoding.GetBytes(val); + Write(buf.Length + 1); + Write(buf); + Write((byte) 0x00); + } + + public void WriteCString(string val) { + Write(_encoding.GetBytes(val)); + Write((byte) 0x00); + } + } +} + |