private String readTag(LineProtocolInputStream is) throws IOException, BadCommandException {
        int c;

        StringBuilder sb = new StringBuilder();
        while ((c = is.read()) != -1) {

            if (c == '\r' || c == '\n') {
                is.unread(c);
                throw new BadCommandException("unexpected end of line");
            }

            if (ImapGrammar.TAG_CHAR.matches((char) c)) {
                sb.append((char) c);
            } else if (c == ' ') {
                if (sb.length() == 0) {
                    throw new BadCommandException("missing tag");
                }
                break;
            } else {
                throw new BadCommandException("invalid tag");
            }
        }

        if (c == -1) {
            throw new EOFException("encountered EOF while reading tag");
        }

        return sb.toString();
    }