Wednesday, July 25, 2018

The Difference Between URI, URL and URN

This small post briefly explains the difference between URI, URL and URN. Sometimes the first two terms are used interchangeably, and the last one is encountered less frequently in software development. Anyway, it is a good idea to understand the difference between them.

URI (Uniform Resource Identifier)

In accordance with Wikipedia, a Uniform Resource Identifier (URI) is a string of characters designed for unambiguous identification of resources and extensibility via the URI scheme. In other words, URI is designed to uniquely identify a resource, but it may lack of information about the location or retrieval protocol of a resource. URI is defined by schemes. Schemes specify concrete syntax and associated protocols. The most known URI type is Uniform Resource Locator (URL). More information about URI can be found here.

There are a few URI examples:
  • http://www.example.com/index.html — URI which is also a URL.
  • data:,Example%20URI — URI which is not a URL (this is a data URI).
  • view-source:http://www.example.com/index.html — another example of URI which is not a URL (this is a URC).

URL (Uniform Resource Locator)

Wikipedia says that a Uniform Resource Locator (web address) is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it. In other words, URL contains two additional pieces of information that may be missing from URI (depending on URI scheme): a location and a retrieval protocol for a resource. This is the key difference between URL and URI. Another important thing to remember: every URL is a URI, but not vice versa. More information about URL can be found here.

There are a few URL examples:
  • mailto:admin@example.com
  • http://www.example.com/index.html
  • /other/link.html — relative URL

URN (Uniform Resource Name)

A Uniform Resource Name is yet another form of URI. It uses 'urn' scheme to identify a resource with a unique and persistent name. More information about URN can be found here.

There are a few URN examples:
  • urn:uuid:1be065ad-43e0-4d34-bd66-6ffc005f2edf — globally unique identifier (GUID).
  • urn:isbn:1231231234 — identifies a book by ISBN.

That's it.
Thanks for reading!

Friday, July 7, 2017

How to Checkout a Specific Tag in Git

Here is a quick tip which shows how to checkout a specific tag in Git.
    $ git clone <repository-link>
    $ git checkout tags/<tag-name> -b <branch-name>
The first command clones the whole repository. The second one takes a specific tag and creates a branch for it. If there are any doubts regarding the tag name, a list of tags can be retrieved via the following command:
    $ git tag -l
Specifying a branch name is actually optional. It defaults to a revision number of a tag which might not be very convenient.

Monday, September 19, 2016

Clone a Git Repository Into a Specific Folder

Let's take a look at a sample git repository URL:
https://github.com/apolunin/java8-impatient
It can be cloned via the following command:
git clone https://github.com/apolunin/java8-impatient
The contents of the repository will be put in the 'java8-impatient' folder which will be created in a current working directory. What if we want a different one? Fortunately this is very easy to do in Git. Here is a quick but useful tip which shows how to clone a Git repository into a different folder:
git clone https://github.com/apolunin/java8-impatient my-impatient-repo
This time the contents of the repository will be put in the 'my-impatient-repo' folder under the current working directory.

Change File Type in Vim

Recently I had to write a few lines of code in assembly language. Vim and GAS were chosen as suitable tools to use. I downloaded vim-gas plugin, installed it using vim-plug (Vim plugin manager that I prefer) and started coding. Unfortunately Vim detected file type as 'vmasm' by default, not as 'gas', so syntax highlighting was incorrect. In this post I describe several solutions to this problem.

Set File Type Manually

The first obvious solution which comes to mind is to set file type manually. This can be done via the following Vim command:
:set filetype=gas
This definitely works, but setting the file type manually every time it is opened seems awkward and impractical. Fortunately there are better alternatives.

Override File Type Using Auto Commands

There are several automatic commands (autocmd) which can be added to .vimrc in order to set file type:
au BufRead,BufNewFile *.s   setfiletype gas
au BufRead,BufNewFile *.s   set filetype=gas
The first option didn't work for me, because it is used to set the file type only if it hasn't been detected yet. As I mentioned earlier, in my case file type was detected as 'vmasm'.

The second option worked as expected and Vim detected the file type as 'gas'.

Override File Type Using Modelines

Another way to influence Vim filetype detection is to use modelines. Looks like the modelines are disabled by default. In order to turn them on the following lines should be added to .vimrc:
set modeline
set modelines=5
The first line enables modelines and the second one sets the max number of modelines which Vim takes into account. Look at the Vim docs for more details.

The following modeline can be added to the *.s file in order to help Vim to detect file type as 'gas':
/* vim: set filetype=gas: */
Apparently modelines approach requires adding a special line in every *.s file while autocmd approach can be configured solely in .vimrc. So I decided to stick to the latter which suits my needs just fine.

That's all there is to it.
Thanks for reading!

Wednesday, May 25, 2016

Convert InputStream to String in Java

Sometimes in Java world it is necessary to convert an InputStream to a String for further processing. There are several ways to do it, but here is a fantastic trick I recently discovered in this article:
final String text = new Scanner(inputStream)
        .useDelimiter("\\A").next();
The idea behind this trick is regex \A. It matches the beginning of input. Since there is only one beginning of input the whole contents of the stream is returned as the first (and the last) token.

There is a problem with the code above. It would throw an exception if the provided stream is empty. However this is easy to fix:
final Scanner scanner = new Scanner(inputStream)
        .useDelimiter("\\A");
final String text = scanner.hasNext() ? scanner.next() : "";
If a specific charset must be used it could be passed as the second argument to the Scanner constructor:
final Scanner scanner = new Scanner(inputStream, "ISO-8859-1")
        .useDelimiter("\\A");
final String text = scanner.hasNext() ? scanner.next() : "";
That's all there is to it.
Thanks for reading!

Friday, April 22, 2016

Create a GUID in Java

Some software development tasks need a way to generate unique identifiers. I saw several homegrown solutions, but Java 5 makes the process a breeze. It contains class UUID which can generate type 4 (pseudo randomly generated) unique identifiers. Here is a simple example:
package net.softwaregeek;

import java.util.UUID;

public class UUIDMain {
    public static void main(final String[] args) {
        System.out.println(UUID.randomUUID());
    }
}
The code is self-explanatory. It generates a unique identifier and prints it to console. Class UUID contains several other useful methods which I encourage you to explore on your own.

Thursday, April 7, 2016

Change Editor for Git Commit Messages

Git is a very powerful tool especially when used from a console. Obviously committing changes is a pretty common task which includes editing commit messages. If a message is short it is easy to specify it via -m option:
$ git commit -m "my commit message"
But what if you have a long list of changes and you need a descriptive well-formatted commit message? It would be cool to write it in your favorite text editor, wouldn't it? Is there a way to tell Git which editor to use? Yes, it is. According to the docs for git-commit:

"The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order)."

I'm a Vim fan and I stick to the core.editor configuration variable. So, here is my preferred way to configure commit message editor:
$ git config --global core.editor "vim"
Now I can omit -m option and enjoy editing commit messages in Vim :)