Mar 2 2011

Wasps(Actually bees) and Asteroids

Category: Codingtonyenkiducx @ 08:17

So I saw the brilliant JS browser code by Eric to add an asteroids type game into a web browser, and I thought I would make my own, and add to his a little..  Click the link below to start adding some wasps(Actually bees) to my site.  You can right-click the link and add as a bookmark and it can be used on any website then the same way as Erics.  When I'd finished it, I dumped a load of wasps(Actually bees) on a website and fired up Erics code as well, and you can actually hunt down and kill the bees!  Awesome coincidence.

 

Bookmark me

Tags:

Feb 12 2011

Compressing strings in memory - .net

Category: Codingtonyenkiducx @ 08:50

I came across an interesting problem recently, while writing something of a web spider.  The data it had to produce was pretty comprehensive and involved cross-page statistics, and the way the information was structured unfortunately meant I had to load pretty much the entire HTML content of a website into memory.  Which worked fine for a lot of sites, it's hardly a lot of data, but when we came to some of the massive sites(90,000 pages+) there was a severe memory problem, with the spider sometimes balooning up to 4GB of ram before a crash occured.

So there were a limited number of solutions.  But in the end the only viable one seemed to be in-memory compression of the text content.  Processor usage was not a huge concern, we were bare spiking to 50% usage on 3 cores of a quad xeon processor. 

After some digging around for zip components, I came across the gZip functions stored within .net3.5+ itself, and the decision was an easy one.  Just for reference, at it's most extreme our service was running up to 4GB of RAM and dying after processing the complete HTML for about 72,000 webpages.  After using this compression functionality on the strings we went down to just over 500MB of ram used to complete the processing of all 90,000+ pages.

Now, for some code..

First a function to take a string and compress it into a byte array

 

 

        Public Shared Function Compress(ByVal data As String) As Byte()
            If data Is Nothing Or data = "" Then
                Return New Byte(0) {}
            End If
            Dim strByteArray As Byte() = StrToByteArray(data)
            Dim output As New MemoryStream()
            Dim gzip As New GZipStream(output, CompressionMode.Compress, True)
            gzip.Write(strByteArray, 0, strByteArray.Length)
            gzip.Close()
            Return output.ToArray()
        End Function

 

 

There's nothing to amazing in there, just standard conversion from a string to a byte array, and then a memory stream to facilitate the transfer to the gZip stream.  There is a function to do string to byte array in there, I'll post that at the bottom.  Next, decompress it.

 

 

        Public Shared Function Decompress(ByVal data As Byte()) As String
            If data Is Nothing Or data.Length = 1 Then
                Return ""
            End If
            Dim input As New MemoryStream()
            input.Write(data, 0, data.Length)
            input.Position = 0
            Dim gzip As New GZipStream(input, CompressionMode.Decompress, True)
            Dim output As New MemoryStream()
            Dim buff As Byte() = New Byte(63) {}
            Dim read As Integer = -1
            read = gzip.Read(buff, 0, buff.Length)
            While read > 0
                output.Write(buff, 0, read)
                read = gzip.Read(buff, 0, buff.Length)
            End While
            gzip.Close()
            Dim readBuff As Byte() = New Byte(63) {}
            output.Position = 0
            Dim strRead As StreamReader = New StreamReader(output)
            Return strRead.ReadToEnd
        End Function

 

 

This is a little more fiddly, because we have to buffer the stream before it comes out, but still all simple stuff.  It's worth noting that this function is not safe from some bad or null data, so check your string inputs before you pass them into the function.  And last but not least, the string to byte array function.

 

 

        Public Shared Function StrToByteArray(ByVal str As String) As Byte()
            Dim encoding As New System.Text.UTF8Encoding()
            Return encoding.GetBytes(str)
        End Function

 

 

All done. If you wanted to integrate this into an existing bit of code, you can inject the functions into a property, like this.

 

 

Private pageContentCompressed As Byte() = New Byte(0) {}

Public Property pageContent() As String
  Get
    Return CommonFunctions.Decompress(pageContentCompressed)
  End Get
  Set(ByVal value As String)
    pageContentCompressed = CommonFunctions.Compress(value)
  End Set
End Property

 

 

 

c# code follows below.

 

 

public static byte[] Compress(string data)
{
    if (data == null | string.IsNullOrEmpty(data)) {
        return new byte[1];
    }
    byte[] strByteArray = StrToByteArray(data);
    MemoryStream output = new MemoryStream();
    GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
    gzip.Write(strByteArray, 0, strByteArray.Length);
    gzip.Close();
    return output.ToArray();
}

public static string Decompress(byte[] data)
{
    if (data == null | data.Length == 1) {
        return "";
    }
    MemoryStream input = new MemoryStream();
    input.Write(data, 0, data.Length);
    input.Position = 0;
    GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
    MemoryStream output = new MemoryStream();
    byte[] buff = new byte[64];
    int read = -1;
    read = gzip.Read(buff, 0, buff.Length);
    while (read > 0) {
        output.Write(buff, 0, read);
        read = gzip.Read(buff, 0, buff.Length);
    }
    gzip.Close();
    byte[] readBuff = new byte[64];
    output.Position = 0;
    StreamReader strRead = new StreamReader(output);
    return strRead.ReadToEnd;
}

public static byte[] StrToByteArray(string str)
{
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    return encoding.GetBytes(str);
}

private byte[] pageContentCompressed = new byte[1];

public string pageContent {
    get { return CommonFunctions.Decompress(pageContentCompressed); }
    set { pageContentCompressed = CommonFunctions.Compress(value); }
}

 

 

 

Tags:

Feb 12 2011

Get motivated! - Scary Bike

Category: tonyenkiducx @ 08:50

Tags:

Feb 12 2011

Peanut butter filled, chocolate covered pretzels

Category: tonyenkiducx @ 08:49

I've had more than a few people ask me for the recipe for my peanut butter filled chocolate covered pretzels, and I'm always too lazy to write it down.
So here it is, in convenient electronic format. This is enough to make about 100, and the process of making them is going to make you hate peanut butter, pretzels and chocolate.

Ingredients :
2 Bags of Small Pretzels(New york brand, or sainsburys are good)
2 jars of Peanut Butter
1 big bar of milk chocolate*
1 big bar of dark chocolate*
2 big bars of white chocolate*

Equipment :
Bowl
4 large flat trays
Tin foil
Large pan, with a bowl that will rest nicely on top of it
Tea spoon
Pair of tweezers

*Dont buy cadburys or cooking chocolate, both suck, and the amount of effort you're going to put into this, it's worth spending the extra few quid for a good quality chocolate.  Sainsburys taste the difference range, or Lindt are both good.  Lindt only sell small bars, so double up the amounts for them.

1) Start off by emptying your pretzels into a large bowl, and removing all of the broken ones.
2) Empty both jars of peanut butter into a bowl
3) Cover 2 large trays with foil
4) Put everything in front of the TV, this will take a while
5) Using one pretzel, scoop up a small amount of peanut butter
6) Pick another pretzel and use it to squish the peanut butter flat against the other pretzel, then press them firmly together
           *If the pretzels dont stick together well, use more peanut butter.  If they squish peanut butter all over you, use less
7) Do *NOT* break the pretzels, if you do, eat it or bin it, you'll thank me later
8) Make sure no peanut butter is sticking out of the holes in the pretzel
9) Place the pretzel on the tray
10) Repeat 100 times
11) When finished, place the trays in the freezer and leave for an hour or more(The longer the better)
12) Have a rest, you deserve it
13) Put your pan on the stove and fill it 3/4 full with water and set off to boil
14) While the water is boiling, put the milk and dark chocolate into a freezer bag or similair, and bash the shit out of it with a rolling pin
15) Cover two more trays in foil
16) When the water is close to boiling lower the heat to absolute minimum
17) Put the chocolate in the bowl and place on top of the pan
18) Do'nt be impatient, let the chocolate melt as slowly as possible, it will taste much better
19) When melted, remove one tray of pretzels from the freezer
20) Using your tweezers grab a pretzel and dunk it in your chocolate
21) Flip it over, make sure it's covered, and place on the empty tray
22) Repeat for the entire first tray, then place the tray of pretzels back in the freezer again
23) Put the remaining chocolate aside, you will need it later
24) Now repeat the process again for the second tray, but this time just use white chocolate
25) When the tray is back in the freezer, grab the tray of dark chocolate pretzels out of the freezer
26) Dip a warm spoon in the white chocolate and flick it back and forth over the pretzels(This take a bit of practice, but they should end up like below)

27) When finished, re-freeze them
28) When the white pretzels are frozen, get them out and repeat with the dark chocolate
29) Leave them for an hour or so, then transfer to bowls and KEEP REFRIGERATED until you eat them

Tags:

Feb 12 2011

Have you ever listened to music? OR How I learned to defend Katy Perry while hating myself

Category: tonyenkiducx @ 08:49

So I was on youtube last night, skipping through some random tunes as I do, and I came across a Katy Perry song, californian girls.  I'm not a huge fan of hers, but the song was ok, the video looked really nice and she was cute as always.  I made the mistake I always make of surfing some comments as I was watching, and I was as usual greeted by the usual fangirls, the haters and what I now think is the stupidest group of them all, the musical snobs.  People who genuinely post just to say that the singer/artist is not a very good singer, and there are far better singers elsewhere and therefore you shouldn't listen to them.

I tended to agree with the comments, they are on the whole correct, most famous artists cannot sing very well.  But is that really the point?  If you actually think about it for a minute, how many of the most famous acts in history were lead by a really great singer?  There are not very many.  And for every example of a quality singer, I can think of 3 or 4 that were not.  For every Fleetwood Mac, there is a Black Sabbath, a Beatles(Yes, even Paul says they were not great singers), BoB Dylan or Neil Young.  Singing quality is not what it is all about, this can be easily proven with a copy of the charts from the last 40 years and a calculator.

I shudder to go back to Ms Perry, but she has some real personality and character.  Yes, she's pretty attractive, but there are a lot of girls who cant sing with large breasts out there, and they're not all famous.  But she is a performer, she knows how to entertain people whilst doing her best to sing in tune and people like that!  Viva la differance!

Tags:

Feb 12 2011

Create a very strong, easy to remember password.

Category: Generaltonyenkiducx @ 08:48

Having a hundred passwords in various places can tax anyone, and having ultra secure passwords is especially difficult.  So I use a method an old sysadmin taught me when I was about 18, and it enables me to use 10 character passwords with all the right combination of character types in them.  They will register as very strong on any test.

Here's an example I just made up;

14 monkeys at Blackpool zoo looking up at the stars

We take the numbers and first letter of each word to form the password, and convert anything sounding like a special character.  In this example we will convert "at" to @ and "stars" to **(StarS being multiples).  So the password will be;

14m@Bzlu@t**

You can even leave yourself a reminder on paper, if you just write down the start of the phrase it will jog your memory.  An even better way is to choose a phrase that means something to you, which I wont do here for obvious reasons.  A couple more examples;

The 23rd earl of Salisbury is worth 12 million pounds

T23eoSiw12m£

My sister, Joanne, has worked at starbucks for 6 months

MsJhw@*$$f6m

 

Hope that helps you remember some new awesome passwords!

Tags:

Feb 12 2011

Will I burn in hell for this one?

Category: tonyenkiducx @ 08:48

I certainely hope so!

 

Tags:

Feb 12 2011

p.bear gets up to more japery

Category: tonyenkiducx @ 08:48

He's such a scamp!

 

 

Tags:

Feb 12 2011

Cows - WTF?

Category: General | Randomtonyenkiducx @ 08:47

Another piece of genius, this time in the form of cow(s).

Tags:

Feb 12 2011

Portal 2 teaser - Meet Wheatley

Category: Generaltonyenkiducx @ 08:47

I have been looking forward to portal 2 since about 15 minutes into the first game, but this has really got me excited now.

 

Tags: