1 // testblob.cs - tests loading a binary file into an oracle blob and vice-versa
2 using System;
3 using System.Data;
4 using System.Data.OracleClient;
5 using System.Text;
6 using System.IO;
7 
8 class TestBlob
9 {
10 	static string infilename = @"../../../tools/mono-win32-setup-dark.bmp";
11 	static string outfilename = @"mono-win32-setup-dark2.bmp";
12 	static string connectionString = "Data Source=testdb;User ID=scott;Password=tiger";
13 	static byte[] bytes1 = null;
14 
Main(string[] args)15 	public static void Main (string[] args)
16 	{
17 		OracleConnection con = new OracleConnection();
18 		con.ConnectionString = connectionString;
19 		con.Open();
20 
21 		BLOBTest (con);
22 		ReadBlob (con);
23 
24 		con.Close();
25 		con = null;
26 	}
27 
28 	// read the BLOB into file "cs-parser2.cs"
ReadBlob(OracleConnection connection)29 	public static void ReadBlob (OracleConnection connection)
30 	{
31 		if (File.Exists(outfilename) == true) {
32 			Console.WriteLine("Filename already exists: " + outfilename);
33 			return;
34 		}
35 
36 		OracleCommand rcmd = connection.CreateCommand ();
37 		rcmd.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST";
38 		OracleDataReader reader2 = rcmd.ExecuteReader ();
39 		if (!reader2.Read ())
40 			Console.WriteLine ("ERROR: RECORD NOT FOUND");
41 
42 		Console.WriteLine ("  TESTING OracleLob OBJECT 2...");
43 		OracleLob lob2 = reader2.GetOracleLob (0);
44 		Console.WriteLine ("  LENGTH: {0}", lob2.Length);
45 		Console.WriteLine ("  CHUNK SIZE: {0}", lob2.ChunkSize);
46 
47 		byte[] lobvalue = (byte[]) lob2.Value;
48 
49 		if (ByteArrayCompare(bytes1, lobvalue) == true)
50 			Console.WriteLine("bytes1 and bytes2 are equal: good");
51 		else
52 			Console.WriteLine("bytes1 and bytes2 are not equal: bad");
53 
54 		FileStream fs = new FileStream(outfilename, FileMode.CreateNew);
55 		BinaryWriter w = new BinaryWriter(fs);
56 		w.Write(lobvalue);
57 		w.Close();
58 		fs.Close();
59 
60 		lob2.Close ();
61 		reader2.Close ();
62 	}
63 
BLOBTest(OracleConnection connection)64 	public static void BLOBTest (OracleConnection connection)
65 	{
66 		Console.WriteLine ("  BEGIN TRANSACTION ...");
67 
68 		OracleTransaction transaction = connection.BeginTransaction ();
69 
70 		Console.WriteLine ("  Drop table BLOBTEST ...");
71 		try {
72 			OracleCommand cmd2 = connection.CreateCommand ();
73 			cmd2.Transaction = transaction;
74 			cmd2.CommandText = "DROP TABLE BLOBTEST";
75 			cmd2.ExecuteNonQuery ();
76 		}
77 		catch (OracleException) {
78 			// ignore if table already exists
79 		}
80 
81 		Console.WriteLine ("  CREATE TABLE ...");
82 
83 		OracleCommand create = connection.CreateCommand ();
84 		create.Transaction = transaction;
85 		create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
86 		create.ExecuteNonQuery ();
87 
88 		Console.WriteLine ("  INSERT RECORD ...");
89 
90 		OracleCommand insert = connection.CreateCommand ();
91 		insert.Transaction = transaction;
92 		insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
93 		insert.ExecuteNonQuery ();
94 
95 		OracleCommand select = connection.CreateCommand ();
96 		select.Transaction = transaction;
97 		select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
98 		Console.WriteLine ("  SELECTING A BLOB (Binary Large Object) VALUE FROM BLOBTEST");
99 
100 		OracleDataReader reader = select.ExecuteReader ();
101 		if (!reader.Read ())
102 			Console.WriteLine ("ERROR: RECORD NOT FOUND");
103 
104 		Console.WriteLine ("  TESTING OracleLob OBJECT ...");
105 		OracleLob lob = reader.GetOracleLob (0);
106 		Console.WriteLine ("  LENGTH: {0}", lob.Length);
107 		Console.WriteLine ("  CHUNK SIZE: {0}", lob.ChunkSize);
108 
109 		//try {
110 			if (File.Exists(infilename) == false) {
111 				Console.WriteLine("Filename does not exist: " + infilename);
112 				return;
113 			}
114 
115 			FileStream fs = new FileStream(infilename, FileMode.Open, FileAccess.Read);
116 			BinaryReader r = new BinaryReader(fs);
117 
118 			byte[] bytes = null;
119 			int bufferLen = 8192;
120 			bytes = r.ReadBytes (bufferLen);
121 
122 			while(bytes.Length > 0) {
123 				Console.WriteLine("byte count: " + bytes.Length.ToString());
124 				lob.Write (bytes, 0, bytes.Length);
125 				bytes1 = ByteArrayCombine (bytes1, bytes);
126 				if (bytes.Length < bufferLen)
127 					break;
128 				bytes = r.ReadBytes (bufferLen);
129 			}
130 
131 			r.Close();
132 			fs.Close ();
133 		//}
134 		//catch (Exception e) {
135 		//	Console.WriteLine("The file could not be read:");
136 		//	Console.WriteLine(e.Message);
137 		//}
138 
139 		lob.Close ();
140 
141 		Console.WriteLine ("  CLOSING READER...");
142 
143 		reader.Close ();
144 		transaction.Commit ();
145 		transaction = null;
146 		lob = null;
147 		reader.Dispose();
148 		reader = null;
149 		create = null;
150 		insert = null;
151 		select = null;
152 	}
153 
ByteArrayCombine(byte[] b1, byte[] b2)154 	static byte[] ByteArrayCombine (byte[] b1, byte[] b2)
155 	{
156 		if (b1 == null)
157 			b1 = new byte[0];
158 		if (b2 == null)
159 			b2 = new byte[0];
160 
161 		byte[] bytes = new byte[b1.Length + b2.Length];
162 		int i = 0;
163 		for (int j = 0; j < b1.Length; j++) {
164 			bytes[i] = b1[j];
165 			i++;
166 		}
167 		for (int k = 0; k < b2.Length; k++) {
168 			bytes[i] = b2[k];
169 			i++;
170 		}
171 		return bytes;
172 	}
173 
ByteArrayCompare(byte[] ba1, byte[] ba2)174         static bool ByteArrayCompare(byte[] ba1, byte[] ba2)
175         {
176             if (ba1 == null && ba2 == null)
177                 return true;
178 
179             if (ba1 == null)
180                 return false;
181 
182             if (ba2 == null)
183                 return false;
184 
185             if (ba1.Length != ba2.Length)
186                 return false;
187 
188             for (int i = 0; i < ba1.Length; i++)
189             {
190 		Console.WriteLine("i: " + i.ToString() + " ba1: " + ba1[i].ToString() + " ba2: " + ba2[i].ToString());
191             }
192 
193             for (int i = 0; i < ba1.Length; i++)
194             {
195                 if (ba1[i] != ba2[i])
196                     return false;
197             }
198 
199             return true;
200         }
201 
202 }
203