Saturday, January 31, 2015

Making jQuery a bit more Angular

One of my favorite features of AngularJS is the use of HTML attributes to apply controllers and directives directly to your DOM elements. Why is this so useful?

  • It is intuitive for developers to discover what code is being applied to elements.
  • It enables generic registration, removing boiler plate document ready methods.
  • It provides hierarchical scope, encouraging single responsibility controls.

jQuery plugins are already designed to be applied to collections of elements, so let's just add the ability to dynamically apply plugins via HTML attributes! This is how we can make jQuery a bit more Angular.

Sample Script

Our sample jQuery plugin is super simple; it just makes an element fade in and out continuously. This is a very simple behavior, but the point is that it is just a jQuery plugin!

(function ($) {
    $.fn.blink = function() {
        var $el = this;
        setInterval(blinkEl, 1000);
        function blinkEl() {

Capture Local Traffic for WireShark, with RawCap

Capturing local network traffic is a difficult task for many tools, including WireShark. Do not worry, there is a very simple solution to this problem!

Just use RawCap!

RawCap is a tiny (23KB) application that can capture local traffic and then write that to a pcap (packet capture) file, which can be opened by tools like WireShark! Please note that when using this you must specify your target address as, not just localhost.


Friday, January 2, 2015

File.ReadAllText with an Offset in .NET

What do you do when you need to read text from a file in .NET, but you want to start from an offset? This is a slightly niche scenario, but it does happen. Below is a solution to that problem.

To summarize the implementation, you open a file stream, seek to your offset, and then read in the bytes from there. While loading the result I read small chunks into a buffer, decoded them, and then added the decoded string to a string buffer for storage. Please note that if you are using a multibyte encoding then this helper will only work if you use the correct offset.

Helper Code

public class FileHelper
    private const int BufferSize = 1024;
    public static string ReadAllTextFromOffset(
        string path, 
        Encoding encoding, 
        int offset, 
        out int totalLength)
        using (var fs = new FileStream(path, FileMode.Open))
            totalLength = offset;
            if (offset > 0)
                fs.Seek(offset, SeekOrigin.Begin);
            var sb = new StringBuilder();
            var buffer = new byte[BufferSize];
            int readCount;
                readCount = fs.Read(buffer, 0, buffer.Length);
                totalLength += readCount;
                var subString = encoding.GetString(buffer, 0, readCount);
            while (readCount == buffer.Length);
            return sb.ToString();
Real Time Web Analytics