title: URL shortening algorithm
body: <p>Now, this is not strictly about URL shortening, but my purpose is such anyway, so let's view it like that. Of course the steps to URL shortening are:</p>
  
  <ol>
  <li>Take the full URL</li>
  <li>Generate a unique short string to be the key for the URL</li>
  <li>Store the URL and the key in a database (a key-value store would be a perfect match here)</li>
  </ol>
  
  <p>Now, about the second point. Here's what I've come up with:</p>
  
  <pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(baos);
  UUID uuid = UUID.randomUUID();
  dos.writeLong(uuid.getMostSignificantBits());
  String encoded = new String(Base64.encodeBase64(baos.toByteArray()), "ISO-8859-1");
  String shortUrlKey = StringUtils.left(encoded, 6); // returns the leftmost 6 characters
  // check if exists in database, repeat until it does not
  </code></pre>
  
  <p>Is this good enough?</p>