You don't need this if statement. Just always do this:

    oStream.write(ptr.get(), ptr.getOffset(), ptr.getLength());