I'm calculating SHA-512 checksum's for video files that will be registered with a custom video asset manager that I wrote. Checksum's are useful fingerprints for identifying a file. In my case the checksums are useful for doing reverse look ups; e.g. if I have a file, I can look up it's metadata just by using it's checksum.

SHA-512 from Command Line

For my application, I can use the linux/mac util shasum when I register the file. In this case the command looks like:
shasum -a 512 -p /path/to/videofile.mp4
For a 40GB file, this takes about 160 seconds to run. For a 4 GB file, it takes 20 seconds.

SHA-512 in Java

However, I also need to calculate the checksum in a Java/Scala application. To do that I used the following script (written in scala)
#!/usr/bin/env scala

// Output matches the shasum -a 512 -p output

import java.io._
import java.math._
import java.security._


val file = new File(args(0))
val h = hash(file)
println(new BigInteger(1, h).toString(16)) // Dump out hash as hex


def hash(file: File): Array[Byte] = {
  val in = new BufferedInputStream(new FileInputStream(file))
  val digest = MessageDigest.getInstance("SHA-512");
  val buffer = Array.ofDim[Byte](1048576) // 1 MB. I tried 4MB and it was the same
  var sizeRead = -1
  var ok = true
  while(ok) {
    sizeRead = in.read(buffer)
    if (sizeRead == -1) ok = false
    else digest.update(buffer, 0, sizeRead)
  }
  in.close()
  
  digest.digest()
}
This takes slightly longer than the command line, but it's pretty close. A 40GB file takes about 185 seconds, a 4GB file takes about 21 seconds. Of course, that also includes the time to compile the code.
If you upgrade to the latest beta of iTerm (v3-ish), you will find that the iTerm support in TextMate is broken. i.e. Trying to launch an interactive shell from TextMate will fail. I wrote up a little bit of AppleScript to fix this issue.

Step 1

Open Script Editor and create a new doc. Paste the following code into it:

(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3/10.4
     
    Written by Brian Schlining
 *)


property debug : false

-- when the toolbar script icon is clicked 
-- 
on run argv
 set this_folder to item 1 of argv
 my process_item(this_folder)
end run



-- this subroutine processes does the actual work 
-- this version can handle this weirdo case: a folder named "te'st"ö te%s`t"

on process_item(this_item)
 
 set thePath to quoted form of POSIX path of this_item
 set theCmd to "cd " & thePath & ";clear;"
 
 tell application "iTerm"
  activate
  -- just open a terminal and cd to thePath
  --set newWindow to (create window with default profile)
  
  set aWindow to current window
  
  if aWindow is equal to missing value then
   set aWindow to (create window with default profile)
  else
   tell aWindow
    set t to (create tab with default profile)
   end tell
  end if
  
  
  tell current session of aWindow
   write text "cd " & thePath & ";clear;"
  end tell
  
 end tell
 
 
end process_item
Save the script somewhere as iTermLaunch.scpt.

Step 2

In TextMate, select "Bundles -> Edit Bundles" from the menu. Then select the "iTerm" bundle. Select Menu Actions in the iTerm bundle. Select "Interactive Shell" and edit the script like so (replace "/Path/to" with the real path to your iTermLaunch.scpt

#!/bin/bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"

# Start a new shell if needed, otherwise show the one with the right name.
#export SHELL_NAME=${SHELL_NAME:="TextMate Shell"}
#"$TM_BUNDLE_SUPPORT/new.sh"

osascript "/Path/to/iTermLaunch.scpt" ${TM_PROJECT_DIRECTORY}

Remember to save the bundle ([CMD]+S)

Step 3

There is no step 3. That's it. Just run that bundle item to open an iTerm shell in your current project.
I just got a notice that iTerm2 has new major upcoming release (v. 3). I upgraded but found that the AppleScript dialect that iTerm used has changed. This change broke my handy little script that launches iTerm from a Finder window. So I took a few minutes to update it. The new code for iTerm2 v3 is below. Just copy and paste it into a new ScriptEditor window and save as iTermHere.scpt. You can then export it as an Application from ScriptEditor and drag it to your Finder toolbar. Here's the code:

(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3/10.4
     
    Written by Brian Schlining
 *)


property debug : false

-- when the toolbar script icon is clicked 
-- 
on run
 tell application "Finder"
  try
   set this_folder to (the target of the front window) as alias
  on error
   set this_folder to startup disk
  end try
  
  my process_item(this_folder)
  
 end tell
end run


-- This handler processes folders dropped onto the toolbar script icon 
-- 
on open these_items
 repeat with i from 1 to the count of these_items
  set this_item to item i of these_items
  my process_item(this_item)
 end repeat
end open


-- this subroutine processes does the actual work 
-- this version can handle this weirdo case: a folder named "te'st"ö te%s`t"

on process_item(this_item)
 
 set thePath to quoted form of POSIX path of this_item
 set theCmd to "cd " & thePath & ";clear;"
 
 tell application "iTerm"
  activate
  -- just open a terminal and cd to thePath
  --set newWindow to (create window with default profile)
  
  set aWindow to current window
  
  if aWindow is equal to missing value then
   set aWindow to (create window with default profile)
  else
   tell aWindow
    set t to (create tab with default profile)
   end tell
  end if
  
  
  tell current session of aWindow
   write text "cd " & thePath & ";clear;"
  end tell
  
 end tell
 
 
end process_item

My goto editor these days is TextMate. (The recent 2.0 releases have been really great). Like many developers, I use git for source control. Most of the time I use git on the command line, but once in a while I'll open up a project in the very awesome, and very free SourceTree VCS Gui from Atlassian. To simplify opening a project in SourceTree when working from TextMate I added a command via TextMate's extremely simple and flexible Bundles. (Which, due to the simplicity and flexibility of bundle's in TextMate, just tromps on anything I've seen in any other text editor, sorry Sublime Text.).

First, you will need to install SourceTree. Once installed, launch it and select SourceTree > Install Command Line Tools to install the stree shell command.

To add the command to TextMate, do the following:

  1. In TextMate, go to Bundles > Edit Bundles
  2. Select the Bundle that you want to add your command to. I created one with my own name, but you can use existing ones.
  3. Hit [command]+N to open a dialog to Create a New Item
  4. In the Create New Item dialog choose Command
  5. In the sidebar, name it Open With SourceTree. Add your favorite key equivalent (I use [command]+. )
  6. Set Save to Nothing
  7. Set Input to Nothing
  8. Set Output to Discard
  9. In the script shell at the bottom enter the following:
    
    #!/usr/bin/env bash
    if [ -d $TM_DIRECTORY ] ; then
      cd $TM_DIRECTORY 
    elif [ -d $TM_PROJECT_DIRECTORY ] ; then
      cd $TM_PROJECT_DIRECTORY
    fi
      
    GIT_ROOT=$(git rev-parse --show-toplevel)
    if [ -d $GIT_ROOT ] ; then
      stree $GIT_ROOT
    fi
      
And that's it. Enjoy.