Wednesday, 02 March 2011 23:34
Darin
DotNet
There are no translations available. In this blog post I will illustrate how you could convert an XHTML page into PDF using the flying-saucer library. This is a Java library so we need to first step would be to convert it to a .NET assembly.
I will use the IKVM.NET Bytecode Compiler (ikvmc.exe) for this purpose. So go ahead and download both the flying-saucer library and the IKVM project. Then run the following command:
ikvmc.exe -target:library -out:CoreRenderer.dll iText-2.0.8.jar core-renderer.jar
This will generate the CoreRenderer.dll assembly which could be used. And finally we would create an application which would use it inside a console application:
One last step is to ensure that you have referenced the following assemblies:

Those assemblies are part of the IKVM project and have dependencies on many other assemblies that are included with it. So the final folder should contain all those:

And that’s pretty much all. When you run the console application it should generate the output PDF file.
Last Updated on Wednesday, 02 March 2011 22:36
Sunday, 21 November 2010 18:01
Darin
DotNet
There are no translations available. Have you ever been in a situation where you needed to upload multiple files to a remote host and pass additional parameters in the request? Unfortunately there’s nothing in the BCL that allows us to achieve this out of the box.
We have the UploadFile method but it is restricted to a single file and doesn’t allow us to pass any additional parameters. So let’s go ahead and write such method. The important part is that this method must comply with RFC 1867 so that the remote web server can successfully parse the information.
First we define a model representing a single file to be uploaded:
public class UploadFile
{
public UploadFile()
{
ContentType = "application/octet-stream";
}
public string Name { get; set; }
public string Filename { get; set; }
public string ContentType { get; set; }
public Stream Stream { get; set; }
}
And here’s a sample UploadFiles method implementation:
public byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
{
var request = WebRequest.Create(address);
request.Method = "POST";
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
request.ContentType = "multipart/form-data; boundary=" + boundary;
boundary = "--" + boundary;
using (var requestStream = request.GetRequestStream())
{
// Write the values
foreach (string name in values.Keys)
{
var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
}
// Write the files
foreach (var file in files)
{
var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
file.Stream.CopyTo(requestStream);
buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
}
var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
}
using (var response = request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var stream = new MemoryStream())
{
responseStream.CopyTo(stream);
return stream.ToArray();
}
}
And here’s a sample usage:
using (var stream1 = File.Open("test.txt", FileMode.Open))
using (var stream2 = File.Open("test.xml", FileMode.Open))
using (var stream3 = File.Open("test.pdf", FileMode.Open))
{
var files = new[]
{
new UploadFile
{
Name = "file",
Filename = "test.txt",
ContentType = "text/plain",
Stream = stream1
},
new UploadFile
{
Name = "file",
Filename = "test.xml",
ContentType = "text/xml",
Stream = stream2
},
new UploadFile
{
Name = "file",
Filename = "test.pdf",
ContentType = "application/pdf",
Stream = stream3
}
};
var values = new NameValueCollection
{
{ "key1", "value1" },
{ "key2", "value2" },
{ "key3", "value3" },
};
byte[] result = UploadFiles("http://localhost:1234/upload", files, values);
}
In this example we are uploading 3 values and 3 files to the remote host.
Next time I will show how to improve this code by adding an asynchronous version using the TPL library in .NET 4.0.
Last Updated on Saturday, 22 January 2011 01:25
Thursday, 12 November 2009 18:41
Darin
DotNet
There are no translations available. Enumerating files in .NET is easy. Everybody knows the GetFiles method and you may be tempted to write code like this: var files = Directory.GetFiles(@"c:\windows\system32", "*.dll");
foreach (var file in files)
{
Console.WriteLine(file);
}
But if you look closer you will notice that this method returns an array of strings. This could be problematic if the directory you search contains lots of files. The method will block until it performs the search and once it finishes it will load all the results into memory. It would be much better if it just returned an IEnumerable. That’s exactly what the EnumerateFiles method does in .NET 4.0. Unfortunately in .NET 3.5 there’s nothing for the job.
In this post I will show how to implement this functionality using the FindFirstFile and FindNextFile functions.
We start by defining the native function prototypes:
internal sealed class Win32Native
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WIN32_FIND_DATA
{
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern SafeFindHandle FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FindNextFile(SafeFindHandle hFindFile, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FindClose(IntPtr hFindFile);
}
You may notice the SafeFindHandle class used in the method signatures. This is just a simple class deriving from SafeHandle that will make sure that unmanaged handle is correctly closed:
[SecurityCritical]
internal class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
{
[SecurityCritical]
public SafeFindHandle() : base(true)
{ }
[SecurityCritical]
protected override bool ReleaseHandle()
{
return Win32Native.FindClose(base.handle);
}
}
As the documentation states the handle created by FindFirstFile needs to be closed with FindClose function. And finally the implementation of the EnumerateFiles method:
public static class DirectoryExtensions
{
public static IEnumerable<string> EnumerateFiles(string path, string searchPattern)
{
// TODO: validate input parameters
string lpFileName = Path.Combine(path, searchPattern);
Win32Native.WIN32_FIND_DATA lpFindFileData;
var handle = Win32Native.FindFirstFile(lpFileName, out lpFindFileData);
if (handle.IsInvalid)
{
int hr = Marshal.GetLastWin32Error();
if (hr != 2 && hr != 0x12)
{
throw new Win32Exception(hr);
}
yield break;
}
if (IsFile(lpFindFileData))
{
var fileName = Path.Combine(path, lpFindFileData.cFileName);
yield return fileName;
}
while (Win32Native.FindNextFile(handle, out lpFindFileData))
{
if (IsFile(lpFindFileData))
{
var fileName = Path.Combine(path, lpFindFileData.cFileName);
yield return fileName;
}
}
handle.Dispose();
}
private static bool IsFile(Win32Native.WIN32_FIND_DATA data)
{
return 0 == (data.dwFileAttributes & 0x10);
}
}
The method could be used like this:
class Program
{
static void Main(string[] args)
{
var files = DirectoryExtensions.EnumerateFiles(@"c:\windows\system32", "*.dll");
foreach (var file in files)
{
Console.WriteLine(file);
}
}
}
Last Updated on Wednesday, 19 May 2010 21:21
Saturday, 03 October 2009 12:32
Darin
DotNet
There are no translations available. In this post I will compare two methods that perform matrix multiplication. We start by defining the Matrix class: class Matrix
{
private readonly double[,] _matrix;
public Matrix(int dim1, int dim2)
{
_matrix = new double[dim1, dim2];
}
public int Height { get { return _matrix.GetLength(0); } }
public int Width { get { return _matrix.GetLength(1); } }
public double this[int x, int y]
{
get { return _matrix[x, y]; }
set { _matrix[x, y] = value; }
}
}
Next we add the first algorithm to the Matrix class which performs a naive multiplication:
public static Matrix NaiveMultiplication(Matrix m1, Matrix m2)
{
Matrix resultMatrix = new Matrix(m1.Height, m2.Width);
for (int i = 0; i < resultMatrix.Height; i++)
{
for (int j = 0; j < resultMatrix.Width; j++)
{
resultMatrix[i, j] = 0;
for (int k = 0; k < m1.Width; k++)
{
resultMatrix[i, j] += m1[i, k] * m2[k, j];
}
}
}
return resultMatrix;
}
The second method uses unsafe code:
public unsafe static Matrix UnsafeMultiplication(Matrix m1, Matrix m2)
{
int h = m1.Height;
int w = m2.Width;
int l = m1.Width;
Matrix resultMatrix = new Matrix(h, w);
unsafe
{
fixed (double* pm = resultMatrix._matrix, pm1 = m1._matrix, pm2 = m2._matrix)
{
int i1, i2;
for (int i = 0; i < h; i++)
{
i1 = i * l;
for (int j = 0; j < w; j++)
{
i2 = j;
double res = 0;
for (int k = 0; k < l; k++, i2 += w)
{
res += pm1[i1 + k] * pm2[i2];
}
pm[i * w + j] = res;
}
}
}
}
return resultMatrix;
}
Now it’s time to measure the performance:
class Program
{
[DllImport("kernel32.dll")]
static extern void QueryPerformanceCounter(ref long ticks);
static long Measure(Action action, int count)
{
long startTicks = 0;
QueryPerformanceCounter(ref startTicks);
for (int i = 0; i < count; i++)
{
action();
}
long endTicks = 0;
QueryPerformanceCounter(ref endTicks);
return endTicks - startTicks;
}
static void Main(string[] args)
{
Random random = new Random();
Matrix m1 = new Matrix(20, 30);
for (int i = 0; i < m1.Height; i++)
{
for (int j = 0; j < m1.Width; j++)
{
m1[i, j] = random.Next(-100, 100);
}
}
Matrix m2 = new Matrix(30, 40);
for (int i = 0; i < m2.Height; i++)
{
for (int j = 0; j < m2.Width; j++)
{
m2[i, j] = random.Next(-100, 100);
}
}
Console.WriteLine(Measure(() => Matrix.NaiveMultiplication(m1, m2), 10000));
Console.WriteLine(Measure(() => Matrix.UnsafeMultiplication(m1, m2), 10000));
}
}
In this test we perform 10000 multiplications of two randomly generated matrices with sizes 20x30 and 30x40 using both methods:
| Method |
CPU cycles |
| unsafe multiplication |
4485698 |
| naive multiplication |
58762273 |
The results show that the naive multiplication is slower by a factor of 13 compared to the multiplication using unsafe code and working directly with memory.
Last Updated on Saturday, 03 October 2009 12:31
Saturday, 03 October 2009 11:56
Darin
DotNet
There are no translations available. Transactional NTFS and DotNet Starting from Windows Vista and Windows Server 2008, Microsoft introduced a great new feature called Transactional NTFS (TxF). It allows developers to write file I/O functions that are guaranteed to either succeed completely or fail completely. Unfortunately there are no classes in the .NET framework that would allows us to perform, such operations. We need to resort to P/Invoke to use the newly introduced functions CreateTransaction, CommitTransaction, RollbackTransaction and DeleteFileTransacted. class Win32Native
{
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
int nLength;
IntPtr lpSecurityDescriptor;
int bInheritHandle;
}
[DllImport("ktmw32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern SafeFileHandle CreateTransaction(SECURITY_ATTRIBUTES securityAttributes, IntPtr guid, int options, int isolationLevel, int isolationFlags, int milliSeconds, string description);
[DllImport("ktmw32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool CommitTransaction(SafeFileHandle transaction);
[DllImport("ktmw32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool RollbackTransaction(SafeFileHandle transaction);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool DeleteFileTransacted(string filename, SafeFileHandle transaction);
}
Note that I am using SafeFileHandle in method signatures instead of IntPtr to hold unmanaged resources which guarantees that they will be properly disposed even if the AppDomain was to stop.
The next thing is to define a helper class which would allow us to easily invoke those functions:
public class FileManager : IDisposable
{
private bool _commited = false;
private SafeFileHandle _tx = null;
public FileManager()
{
_tx = Win32Native.CreateTransaction(new Win32Native.SECURITY_ATTRIBUTES(), IntPtr.Zero, 0, 0, 0, 0, null);
}
public bool DeleteFile(string filename)
{
return Win32Native.DeleteFileTransacted(filename, _tx);
}
public void Commit()
{
if (Win32Native.CommitTransaction(_tx))
_commited = true;
}
private void Rollback()
{
Win32Native.RollbackTransaction(_tx);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (!_commited)
{
Rollback();
}
}
}
public void Dispose()
{
Dispose(true);
}
}
Sample with delete two files
Now suppose that you need to atomically delete two files: either both files are deleted or neither of them, but you never want to have only one of the two files deleted:
using (FileManager manager = new FileManager())
{
manager.DeleteFile("file1.txt");
Console.WriteLine("file1.txt is marked for deletion in current transaction. Press Enter...");
Console.ReadLine();
//throw new Exception("something very bad happens here");
manager.DeleteFile("file2.txt");
Console.WriteLine("file2.txt is marked for deletion in current transaction.");
manager.Commit();
}
The method DeleteFile marks the file for deletion in the current transaction, but it will physically delete it only when the Commit method is called. Thanks to TxF and distributed transactions file and SQL operations can be performed inside the same transaction.
In part 2 we will see how to use the CreateFileTransacted function to perform atomic file read/writes.
Last Updated on Tuesday, 18 May 2010 22:09
Saturday, 03 October 2009 10:44
Darin
DotNet
There are no translations available. Using reflection to invoke methods which are not known at compile time might be problematic in performance critical applications. It is roughly 2.5-3.0 times slower than direct method calls. Here’s a sample test I’ve conducted: class Program
{
[DllImport("kernel32.dll")]
static extern void QueryPerformanceCounter(ref long ticks);
static PropertyInfo _intProp = typeof(Foo).GetProperty("IntProp", BindingFlags.Public | BindingFlags.Instance);
static void Main(string[] args)
{
Foo foo = new Foo { IntProp = 10 };
const int COUNT = 1;
Console.WriteLine(Measure(() => ReadPropertyWithReflection(foo), COUNT));
Console.WriteLine(Measure(() => ReadPropertyDirectly(foo), COUNT));
}
static void ReadPropertyWithReflection(Foo foo)
{
int intProp = (int)_intProp.GetValue(foo, null);
}
static void ReadPropertyDirectly(Foo foo)
{
int intProp = foo.IntProp;
}
static long Measure(Action action, int count)
{
long startTicks = 0;
QueryPerformanceCounter(ref startTicks);
for (int i = 0; i < count; i++)
{
action();
}
long endTicks = 0;
QueryPerformanceCounter(ref endTicks);
return endTicks - startTicks;
}
class Foo
{
public int IntProp { get; set; }
}
}
Here are the results:
| Method |
CPU units |
| Direct method invocation |
796 |
| Reflection method invocation |
1986 |
So using reflection to perform a single property read is 2.5 slower than direct property access.
Dynamic methods can be used to generate and execute a method at run time, without having to declare a dynamic assembly and a dynamic type to contain the method. They are the most efficient way to generate and execute small amounts of code. Here’s an example of using DynamicMethod class to generate a getter for a given property:
static Func<Arg0, TReturn> EmitGetter<Arg0, TReturn>(PropertyInfo propertyInfo)
{
MethodInfo mi = propertyInfo.GetGetMethod();
DynamicMethod dm = new DynamicMethod(
"_get",
typeof(TReturn),
new Type[] { typeof(Arg0) },
propertyInfo.DeclaringType);
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
il.EmitCall(OpCodes.Callvirt, mi, null);
il.Emit(OpCodes.Ret);
return (Func<Arg0, TReturn>)dm.CreateDelegate(typeof(Func<Arg0, TReturn>));
}
Now using this method we can emit a getter method from a PropertyInfo at run time and execute the returned delegate:
Func<Foo, int> getter = EmitGetter<Foo, int>(_intProp);
Console.WriteLine(Measure(() => getter(foo), COUNT));
And here are the final results I’ve obtained using three different methods to read a property value:
| Method |
CPU units |
| Direct method invocation |
796 |
| Dynamic method invocation |
1190 |
| Reflection method invocation |
1986 |
Last Updated on Wednesday, 19 May 2010 21:12
Saturday, 03 October 2009 09:49
Darin
DotNet
There are no translations available. LINQ to XML makes it relatively easy to read and query XML files. For example consider the following XML file: xml version="1.0" encoding="utf-8" ?> <users> <user name="User1" groupid="4" /> <user name="User2" groupid="1" /> <user name="User3" groupid="3" /> <user name="User4" groupid="1" /> <user name="User5" groupid="1" /> <user name="User6" groupid="2" /> <user name="User7" groupid="1" /> </users>
Suppose you would like to find all records with groupid > 2. You could be tempted to issue the following query:
XElement doc = XElement.Load("users.xml"); var users = from u in doc.Elements("user") where u.Attribute("groupid") != null && int.Parse(u.Attribute("groupid").Value) > 2 select u; Console.WriteLine("{0} users match query", users.Count());
There’s a flaw in this method. XElement.Load method will load the whole XML file in memory and if this file is quite big, not only the query might take long time to execute but it might fail running out of memory. If we had some really large XML files we need to buffer through it instead of reading the whole contents into memory. XmlReader is a nice alternative to allowing us to have only the current record into memory which could hugely improve performance. We start by defining a User class which will be used to represent a single record:
public class User { public string Name { get; set; } public int GroupId { get; set; } }
Next we extend the XmlReader class with the User method:
public static IEnumerable<User> Users(this XmlReader source) { while (source.Read()) { if (source.NodeType == XmlNodeType.Element && source.Name == "user") { int groupId; int.TryParse(source.GetAttribute("groupid"), out groupId); yield return new User { GroupId = groupId, Name = source.GetAttribute("name") }; } } }
And finally we can execute the query:
using (XmlReader reader = XmlReader.Create("users.xml")) { var users = from u in reader.Users() where u.GroupId > 2 select u; Console.WriteLine("{0} users match query", users.Count()); }
Conclusion: the second approach runs faster and uses much less memory than the first. The difference is noticeable on large XML files. So if you have to deal with large XML files be cautious when using LINQ to XML.
Last Updated on Wednesday, 19 May 2010 21:07
|