private static void write(DataOutput out, ByteSize size, long value) throws IOException {
    switch (size) {
      case EIGHT:
        out.writeLong(value);
        break;
      case FOUR:
        if (value > Integer.MAX_VALUE) {
          throw new IOException("Integer overflow : " + value);
        }
        out.writeInt((int) value);
        break;
      case TWO:
        if (value > Character.MAX_VALUE) {
          throw new IOException("Character overflow : " + value);
        }
        out.writeChar((int) value);
        break;
      case ONE:
        if (value > Byte.MAX_VALUE) {
          throw new IOException("Byte overflow : " + value);
        }
        out.writeByte((int) value);
        break;
      case ZERO:
        if (value > 0) {
          throw new IOException("Expected empty value!" + value);
        }
        break;
    }
  }