ColdFusion, .Net (dotnet) and byte arrays

Problem: Using ColdFusion's integration with .Net you can use 'System.IO.File' to read in a file and get back the byte array of the file, however it is not a a byte array that you'd expect i.e. a binary object. It is actually translated to an CF Array. This makes it difficult to use CF to deal with .Net byte arrays.

Solution: However you can use .Net's 'System.Convert' to convert it to a Base64 string, something CF can deal with.

See code below:

view plain print about
1<cfsetting showdebugoutput="false">
2
3<!--- The file to read --->
4<cfset FilePath = "C:\Projects\...\AnImage.jpg">
5
6<!--- Read the file using .Net, returns a byte array. --->
7<cfset objFile = CreateObject("dotnet","System.IO.File")>
8<cfset binaryFileDN = objFile.ReadAllBytes(FilePath)>
9
10<!--- Read[binary] the same file using cffile. --->
11<cffile action="readbinary" file="#FilePath#" variable="binaryFileCF">
12
13<!--- Show the 'lengths' of both objects. --->
14<cfoutput>
15CF binary length: #ArrayLen(binaryFileCF)#<br />
16.Net binary length: #ArrayLen(binaryFileDN)#<br /><br />
17</cfoutput>
18
19<!--- Show the dumps of each object. --->
20<cfdump var="#binaryFileCF#" label="CF"><br />
21<cfdump var="#binaryFileDN#" top="10" label=".Net"><br />
22
23<!--- Take the .Net byte array and convert it to a Base64 string. --->
24<cfset Convert = CreateObject("dotnet","System.Convert")>
25<cfset BodyBase64 = Convert.ToBase64String(binaryFileDN)>
26<cfset newBinaryFileDN = BinaryDecode(BodyBase64, "base64")>
27
28<cfdump var="#newBinaryFileDN#" label=".Net">

The result looks something like this:

Of course this is not limited to just reading binary files, anything that .Net returns as a byte array (e.g. 'System.Net.WebClient' DownloadData()) and consequently gets translated by Java/ColdFusion as an 'Array' can be treated the same way.

ColdFusion, .Net (dotnet) and NTLM

Problem: Trying to make an HTTP request against a web site that has Intergated Windows Authentication (IWA) aka NTLM - sometimes CFHTTP just doesn't cut it.

[More]

Convert epoch date to human readable date

I have a function that takes two arguments an epoch date and a date mask and it returns the formatted date.

The line that does all the work is:

view plain print about
1<cfset sFormattedDate = DateFormat( DateAdd("s",left(arguments.epoch,10),DateConvert("utc2Local", "January 1 1970 00:00")), arguments.mask ) >

This works ... for epoch dates less than 19 January 2038.

My application recently started throwing an error when trying to convert 2177456400 to a date. So I needed a more robust way of converting an epoch date to an actual date.

view plain print about
1<cfset dateFormatter = CreateObject("java","java.text.SimpleDateFormat").init(arguments.mask)>
2<cfset date = CreateObject( "java", "java.util.Date" ).init(arguments.epoc*1000)>
3<cfset sFormattedDate = dateFormatter.format(date)>

2177456400 is 01 Jan 2039.

Google getting into the wedding spirit

Just seen that Google have added Mr and Mrs Pegman to Google StreetView for Wills and Kate.

SQL to remove duplicates, keep most recent record

First off, I am not a DBA, although as a developer I have to write a lot of SQL. I have found, when writing SQL, especially when I am after a complicated dataset/recordset, that I have to get into the right mind set.

Anyway I was faced with a problem where an existing database table had duplicate records. I wanted to delete the duplicates but keep the most recent record.

[More]

Recurse through folder structure to a specific depth

Problem: A user at work wanted to copy an existing folder structure 3 levels deep and exclude all files. It would have been time consuming to create the folders manually and we couldn't copy 'n paste the folder structure because there was about 120GB of data and about 63,000 folders!

[More]

How to get local drive information programmatically

Problem: You want to find out how much free space you have on your server's local drives. You also want to find out how much space you have used on your local drives. How can you do this programmatically?

[More]

FireFox 2 Default Theme

Just in case you, like me, aren't really a fan of the default FireFox 2 'Theme' it is easy enough to change.

Go to https://addons.mozilla.org/firefox/3479/ and download the "Winestripe" theme this is the theme that FireFox 1.5 used. Also you may not like the placing of the 'Close' tab buttons. To change this go to 'about:config' in your FF browser address bar scroll down to 'browser.tabs.closeButtons' and change the value. The options are:

0: Close appears on current open tab
1: Close appears on the right of each tab (default)
2: Close dissappears (middle click to close)
3: Close appears on the right of the browser

Happy customising FireFox :)