private static IEnumerable <byte> Enumerate(SqlBytes input, long offset, long count) { switch (input.Storage) { case StorageState.Buffer: return(input.Buffer.Enumerate((int)offset, (int)count)); case StorageState.Stream: return(input.Stream.Locate(offset).ReadBytes().Take(count)); default: throw new NotSupportedException("Unsupported input storage type."); } }
public static SqlBytes BinaryCompress(SqlBytes inputStream) { using (MemoryStream ms = new MemoryStream()) { using (GZipStream x = new GZipStream(ms, CompressionMode.Compress, true)) { byte[] inputBytes = (byte[])inputStream.Value; x.Write(inputBytes, 0, inputBytes.Length); } return(new SqlBytes(ms.ToArray())); } }
private static void CombinatorialBooleanFillRow(object obj, out SqlBytes Value) { if (obj == null) { Value = SqlBytes.Null; } else { var combin = ((Combinatorial <Boolean>)obj).Select(t => (Boolean?)t).ToArray(); var result = new SqlBytes(new MemoryStream()); using (new NulBooleanStreamedArray(result.Stream, System.SizeEncoding.B4, combin, true, false)); Value = result; } }
public void SqlBytesLength() { byte[] b = null; SqlBytes bytes = new SqlBytes(); Assert.Throws <SqlNullValueException>(() => bytes.Length); bytes = new SqlBytes(b); Assert.Throws <SqlNullValueException>(() => bytes.Length); b = new byte[10]; bytes = new SqlBytes(b); Assert.Equal(10, bytes.Length); }
/// <summary> /// Extends Read so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// sqlbytes.Read(offset, buffer); /// </example> /// </summary> public static Int64 Read(this SqlBytes sqlbytes, Int64 offset, Byte[] buffer) { if (sqlbytes == null) { throw new ArgumentNullException("sqlbytes"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } return(sqlbytes.Read(offset, buffer, 0, buffer.Length)); }
public RegistryKey( SqlInt32 osObjectId, SqlString name, SqlString longName, SqlBytes ownerSid, SqlString ownerName ) { m_Id = osObjectId.Value; m_Name = name.Value; m_LongName = longName.Value; m_OwnerSid = new Sid(ownerSid.Value); m_OwnerName = ownerName.Value; }
public void FillFromBytes(SqlBytes value) { if (value.IsNull) { fIsNull = true; return; } byte[] bytes = value.Value; FillFromBytesInternal(bytes); fIsNull = false; return; }
protected override void Dispose(bool disposing) { // When m_sb is null, it means the stream has been closed, and // any opearation in the future should fail. // This is the only case that m_sb is null. try { _sb = null !; } finally { base.Dispose(disposing); } }
/// <summary> /// Extends Write so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// sqlbytes.Write(offset, buffer); /// </example> /// </summary> public static void Write(this SqlBytes sqlbytes, Int64 offset, Byte[] buffer) { if (sqlbytes == null) { throw new ArgumentNullException("sqlbytes"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } sqlbytes.Write(offset, buffer, 0, buffer.Length); }
public static SqlBoolean VerifyHash(SqlBytes input, SqlBytes hash, [SqlFacet(MaxSize = 128)] SqlString algorithmName) { if (input.IsNull || hash.IsNull || algorithmName.IsNull) { return(SqlBoolean.Null); } HashAlgorithm hashAlgorithm = HashAlgorithm.Create(algorithmName.Value); if (hashAlgorithm == null) { return(SqlBoolean.Null); } return(new SqlBoolean(hashAlgorithm.ComputeHash(input.Stream).SequenceEqual(hash.Value))); }
public static SqlBytes ComputeHash(SqlBytes input, [SqlFacet(MaxSize = 128)] SqlString algorithmName) { if (input.IsNull || algorithmName.IsNull) { return(SqlBytes.Null); } HashAlgorithm hashAlgorithm = HashAlgorithm.Create(algorithmName.Value); if (hashAlgorithm == null) { return(SqlBytes.Null); } return(new SqlBytes(hashAlgorithm.ComputeHash(input.Stream))); }
private static void PutBinary(WebRequest request, SqlBytes bytes, NameValueCollection attributes) { using (var stream = request.GetRequestStream()) { long offset = 0L; var buffer = new byte[SqlRuntime.IoBufferSize]; while (offset < bytes.Length) { int read = (int)bytes.Read(offset, buffer, 0, buffer.Length); stream.Write(buffer, 0, read); offset += read; } stream.Flush(); } }
public static SqlBytes Replicate(SqlBytes input, SqlInt64 count) { if (input.IsNull || count.IsNull) { return(SqlBytes.Null); } SqlBytes result = CreateResult(input.Length * count.Value); for (int offset = 0, length = (int)input.Length, c = (int)count.Value; c > 0; offset += length, c--) { input.Read(0L, result.Buffer, offset, length); } return(result); }
public void SqlBytesSetNull() { byte [] b1 = new byte [10]; SqlBytes bytes = new SqlBytes(b1); Assert.AreEqual(bytes.Length, 10, "#1 Should be same"); bytes.SetNull(); try { Assert.AreEqual(bytes.Length, 10, "#1 Should not be same"); Assert.Fail("Should throw SqlNullValueException"); } catch (Exception ex) { Assert.AreEqual(typeof(SqlNullValueException), ex.GetType(), "Should throw SqlNullValueException"); } Assert.AreEqual(true, bytes.IsNull, "#2 Should be same"); }
public static void fn_WriteToFile(string fileName, SqlBytes blob) { if (blob.IsNull || String.IsNullOrEmpty(fileName)) { return; } String fullFileName = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), fileName); using (FileStream fileStream = new FileStream(fullFileName, FileMode.OpenOrCreate, FileAccess.Write)) using (BinaryWriter binaryWriter = new BinaryWriter(fileStream)) { binaryWriter.Write(blob.Buffer); } }
public SqlInt64 WriteAllBinary([SqlFacet(MaxSize = -1)] SqlBytes bytes) { if (IsNull || bytes.IsNull) { return(SqlInt64.Null); } using (var stream = _fi.Create()) { Int64 position = stream.Position; stream.WriteSqlBytes(bytes); stream.Flush(); return(stream.Position - position); } }
public void SqlBytesSetLength() { byte[] b1 = new byte[10]; SqlBytes bytes = new SqlBytes(); Assert.Throws <SqlTypeException>(() => bytes.SetLength(20)); bytes = new SqlBytes(b1); Assert.Equal(10, bytes.Length); Assert.Throws <ArgumentOutOfRangeException>(() => bytes.SetLength(-1)); Assert.Throws <ArgumentOutOfRangeException>(() => bytes.SetLength(11)); bytes.SetLength(2); Assert.Equal(2, bytes.Length); }
private DbGeography GetValueByIndex(int columnIndex, IDataReader dataReader) { byte[] dbBytes = null; int bufferLength = (int)dataReader.GetBytes(columnIndex, 0, null, 0, 0); // initialize it byte[] byteArray = new byte[bufferLength]; // fill it dataReader.GetBytes(columnIndex, 0, byteArray, 0, bufferLength); var sqlBytes = new SqlBytes(byteArray); //dbBytes = SqlGeography.Deserialize(sqlBytes).AsBinaryZM().Value; return(DbGeography.FromBinary(dbBytes)); }
public static SqlBytes ToSqlBytes(object value) { SqlBytes vSqlBytes = null; if (value == DBNull.Value || value == null) { vSqlBytes = null; } else { byte[] vBytes = (byte[])value; vSqlBytes = new SqlBytes(vBytes); } return(vSqlBytes); }
public static SqlBytes xfnGZip(SqlBytes data) { if (data.Length == 0L) { return(SqlBytes.Null); } byte[] raw = data.Value; MemoryStream memory = null; using (memory = new MemoryStream()) using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true)) { gzip.Write(raw, 0, raw.Length); } return(new SqlBytes(memory.ToArray())); }
public void SqlBytesItem() { SqlBytes bytes = new SqlBytes(); Assert.Throws <SqlNullValueException>(() => bytes[0]); byte[] b = null; bytes = new SqlBytes(b); Assert.Throws <SqlNullValueException>(() => bytes[0]); b = new byte[10]; bytes = new SqlBytes(b); Assert.Equal(0, bytes[0]); Assert.Throws <ArgumentOutOfRangeException>(() => bytes[-1]); Assert.Throws <ArgumentOutOfRangeException>(() => bytes[10]); }
public static SqlBytes xfnGUnzip(SqlBytes data) { SqlBytes result; using (MemoryStream memoryStream = new MemoryStream(data.Value)) using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) using (MemoryStream memoryStream2 = new MemoryStream()) { for (int num = gZipStream.ReadByte(); num != -1; num = gZipStream.ReadByte()) { memoryStream2.WriteByte((byte)num); } result = new SqlBytes(memoryStream2.ToArray()); } return(result); }
private SqlBytes ReadConfigFromDatabase(string fileName) { SqlBytes binaryData = null; { // limited scope for variables declared in it SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand command = connection.CreateCommand(); SqlDataReader reader = null; command.CommandType = CommandType.Text; command.CommandText = "SELECT BinaryData FROM Config WHERE FileName = @FileName ORDER BY PartNo ASC"; command.Parameters.AddWithValue("FileName", fileName); try { connection.Open(); reader = command.ExecuteReader(); while (reader.Read()) { binaryData = reader.GetSqlBytes(0); } } catch (Exception error) { // TODO: log error } finally { if (reader != null) { if (reader.HasRows) { command.Cancel(); } reader.Dispose(); } if (command != null) { command.Dispose(); } if (connection != null) { connection.Dispose(); } } } // end of limited scope return(binaryData); }
public static SqlString BlobToNVarChar(SqlBytes sqlBytes) { SqlString str = ""; if (sqlBytes.IsNull) { return(null); } try { Stream bStream = sqlBytes.Stream; byte[] bytes = new byte[bStream.Length]; Encoding Rus = Encoding.GetEncoding("windows-1251"); int n = bStream.Read(bytes, 0, (int)bStream.Length); if (n > 4) { byte[] MAGIC_NUMBER = new byte[] { 0x02, 0x45, 0x7D, 0x5B }; if (bytes[0] == MAGIC_NUMBER[0] && bytes[1] == MAGIC_NUMBER[1] && bytes[2] == MAGIC_NUMBER[2] && bytes[3] == MAGIC_NUMBER[3]) { byte[] bytesOffset = new byte[bytes.Length - 4]; Buffer.BlockCopy(bytes, 4, bytesOffset, 0, bytes.Length - 4); MemoryStream inStream = new MemoryStream(bytesOffset); DeflateStream decompressionStream = new DeflateStream(inStream, CompressionMode.Decompress); MemoryStream outStream = new MemoryStream(); decompressionStream.CopyTo(outStream); str = Rus.GetString(outStream.ToArray()); } else { str = Rus.GetString(bytes, 0, n); } } else { str = Rus.GetString(bytes, 0, n); } } catch { str = null; } return(str); }
public static void TestUdtZeroByte() { using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString)) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = "select hierarchyid::Parse('/') as col0"; using (SqlDataReader reader = command.ExecuteReader()) { Assert.True(reader.Read()); Assert.False(reader.IsDBNull(0)); SqlBytes sqlBytes = reader.GetSqlBytes(0); Assert.False(sqlBytes.IsNull, "Expected a zero length byte array"); Assert.True(sqlBytes.Length == 0, "Expected a zero length byte array"); } } }
public static SqlByte ToByte(SqlBytes input, SqlInt64 index) { if (input.IsNull || index.IsNull) { return(SqlByte.Null); } if (input.Length < sizeof(Byte)) { throw new ArgumentException("Binary has too small size.", "input"); } if (index.Value < 0 || index.Value > input.Length - sizeof(Byte)) { throw new ArgumentOutOfRangeException("index"); } return(new SqlByte(input[index.Value])); }
private SqlBytes GetDBNamesFromDatabase() { SqlBytes binaryData = null; { // limited scope for variables declared in it - using statement does like that - used here to get control over catch block SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand command = connection.CreateCommand(); SqlDataReader reader = null; command.CommandType = CommandType.Text; command.CommandText = "SELECT BinaryData FROM Params WHERE FileName = N'DBNames'"; try { connection.Open(); reader = command.ExecuteReader(); if (reader.Read()) { binaryData = reader.GetSqlBytes(0); } } catch (Exception error) { // TODO: log error } finally { if (reader != null) { if (reader.HasRows) { command.Cancel(); } reader.Dispose(); } if (command != null) { command.Dispose(); } if (connection != null) { connection.Dispose(); } } } // end of limited scope return(binaryData); }
//public static SqlBytes SerializeSqlGeographyMultiPoint(Point2dCollection points2d) //{ // SqlGeometryBuilder buildpoint = new SqlGeometryBuilder(); // buildpoint.SetSrid(0); // buildpoint.BeginGeometry(OpenGisGeometryType.MultiPoint); // foreach (Point2d point in points2d) // { // buildpoint.AddLine(point.X, point.Y, 0, null); // } // buildpoint.EndFigure(); // buildpoint.EndGeometry(); // return buildpoint.ConstructedGeometry.Serialize(); //} public static Point3dCollection DerializeSqlGeographyMultiPoint(SqlBytes points) { SqlGeometry wktpoints = SqlGeometry.Deserialize(points); if (wktpoints == null) { throw new ArgumentNullException(nameof(wktpoints)); } Point3dCollection point3D = new Point3dCollection(); for (int n = 1; n < wktpoints.STNumPoints(); n++) { SqlGeometry pnt = wktpoints.STPointN(n); point3D.Add(new Point3d((double)pnt.STX, (double)pnt.STY, (double)pnt.Z)); } return(point3D); }
private static void Copy(SqlBytes source, long srcOffset, SqlBytes destination, long dstOffset, long length) { if (length == 0) { return; } byte[] buffer = new byte[Comparable.Min(SqlRuntime.IoBufferSize, length)]; long read = 0; while (length > 0 && (read = source.Read(srcOffset, buffer, 0, (int)Comparable.Min(length, buffer.Length))) > 0L) { destination.Write(dstOffset, buffer, 0, (int)read); length -= read; srcOffset += read; dstOffset += read; } }
public void SqlBytesItem() { SqlBytes bytes = new SqlBytes(); try { Assert.Equal(bytes[0], 0); Assert.False(true); } catch (Exception ex) { Assert.Equal(typeof(SqlNullValueException), ex.GetType()); } byte[] b = null; bytes = new SqlBytes(b); try { Assert.Equal(bytes[0], 0); Assert.False(true); } catch (Exception ex) { Assert.Equal(typeof(SqlNullValueException), ex.GetType()); } b = new byte[10]; bytes = new SqlBytes(b); Assert.Equal(bytes[0], 0); try { Assert.Equal(bytes[-1], 0); Assert.False(true); } catch (Exception ex) { Assert.Equal(typeof(ArgumentOutOfRangeException), ex.GetType()); } try { Assert.Equal(bytes[10], 0); Assert.False(true); } catch (Exception ex) { Assert.Equal(typeof(ArgumentOutOfRangeException), ex.GetType()); } }
public void AddVarBinaryMax(SqlString name, SqlBytes value) { base.AddParam(name, value); }
// This doesn't work as SqlBytes are represented as SqlBinary public void SqlTypes_SqlBytes () { NpgsqlParameter parameter; SqlBytes value = new SqlBytes (new byte [] { 0x0d, 0x0a }); parameter = new NpgsqlParameter (); parameter.NpgsqlValue = value; Assert.AreEqual (NpgsqlDbType.VarBinary, parameter.NpgsqlDbType, "#A:NpgsqlDbType"); Assert.AreSame (value, parameter.NpgsqlValue, "#A:NpgsqlValue"); Assert.AreSame (value, parameter.Value, "#A:Value"); parameter = new NpgsqlParameter (); parameter.NpgsqlValue = SqlBytes.Null; Assert.AreEqual (NpgsqlDbType.VarBinary, parameter.NpgsqlDbType, "#B:NpgsqlDbType"); Assert.IsNotNull (parameter.NpgsqlValue, "#B:SqlValue1"); Assert.AreEqual (typeof (SqlBytes), parameter.NpgsqlValue.GetType (), "#B:SqlValue2"); Assert.IsTrue (((SqlBytes) parameter.NpgsqlValue).IsNull, "#B:SqlValue3"); Assert.IsNotNull (parameter.Value, "#B:Value1"); Assert.AreEqual (typeof (SqlBytes), parameter.Value.GetType (), "#B:Value2"); Assert.IsTrue (((SqlBytes) parameter.Value).IsNull, "#B:Value3"); parameter = new NpgsqlParameter (); parameter.Value = value; Assert.AreEqual (NpgsqlDbType.VarBinary, parameter.NpgsqlDbType, "#C:NpgsqlDbType"); Assert.AreEqual (value, parameter.NpgsqlValue, "#C:NpgsqlValue"); Assert.AreEqual (value, parameter.Value, "#C:Value"); }