public int read() throws IOException { int ret = 0; int first, second; do { first = in.read(); if (first == -1) { return -1; } } while (Character.isWhitespace((char) first)); second = in.read(); if (second == -1) { return -1; } ret |= parseNibble((char) first) << 8; ret |= parseNibble((char) second); return ret; } private int parseNibble(char c) throws IOException { if ('0' <= c && c <= '9') { return c - '0'; } else if ('a' <= c && c <= 'f') { return c - 'a'; } else if ('A' <= c && c <= 'F') { return c - 'A'; } else { throw new IOException("Invalid character " + c); } }