public ByteBuffer write(Object obj, ByteOrder order) throws IOException { int size = size(); if (_type == DataType.OLE || _type == DataType.MEMO) { size += ((byte[]) obj).length; } if (_type == DataType.TEXT) { size = getLength(); } ByteBuffer buffer = ByteBuffer.allocate(size); buffer.order(order); if (obj instanceof Boolean) { obj = ((Boolean) obj) ? 1 : 0; } if (_type == DataType.BOOLEAN) {  } else if (_type == DataType.BYTE) { buffer.put(obj != null ? ((Number) obj).byteValue() : (byte) 0); } else if (_type == DataType.INT) { buffer.putShort(obj != null ? ((Number) obj).shortValue() : (short) 0); } else if (_type == DataType.LONG) { buffer.putInt(obj != null ? ((Number) obj).intValue() : 0); } else if (_type == DataType.DOUBLE) { buffer.putDouble(obj != null ? ((Number) obj).doubleValue() : (double) 0); } else if (_type == DataType.FLOAT) { buffer.putFloat(obj != null ? ((Number) obj).floatValue() : (float) 0); } else if (_type == DataType.SHORT_DATE_TIME) { if (obj instanceof Date) {   Calendar cal = Calendar.getInstance(); cal.setTime((Date) obj); long ms = cal.getTimeInMillis(); ms += (long) TimeZone.getDefault().getOffset(ms); buffer.putDouble((double) ms / MILLISECONDS_PER_DAY + DAYS_BETWEEN_EPOCH_AND_1900); } else { buffer.putDouble(0d); } } else if (_type == DataType.BINARY) { buffer.put((byte[]) obj); } else if (_type == DataType.TEXT) { CharSequence text = (CharSequence) obj; int maxChars = size / 2; if (text.length() > maxChars) { text = text.subSequence(0, maxChars); } buffer.put(encodeText(text)); } else if (_type == DataType.OLE) { buffer.put(writeLongValue((byte[]) obj)); } else if (_type == DataType.MEMO) { buffer.put(writeLongValue(encodeText((CharSequence) obj).array())); } else { throw new IOException("Unsupported data type: " + _type); } buffer.flip(); return buffer;
public ByteBuffer write(Object obj) throws IOException { return write(obj, ByteOrder.LITTLE_ENDIAN);
  public ByteBuffer write(Object obj, ByteOrder order) throws IOException {    int size = size();    if (_type == DataType.OLE || _type == DataType.MEMO) {      size += ((byte[]) obj).length;    }    if (_type == DataType.TEXT) {      size = getLength();    }    ByteBuffer buffer = ByteBuffer.allocate(size);    buffer.order(order);    if (obj instanceof Boolean) {      obj = ((Boolean) obj) ? 1 : 0;    }    if (_type == DataType.BOOLEAN) {      //Do nothing    } else if (_type == DataType.BYTE) {      buffer.put(obj != null ? ((Number) obj).byteValue() : (byte) 0);    } else if (_type == DataType.INT) {      buffer.putShort(obj != null ? ((Number) obj).shortValue() : (short) 0);    } else if (_type == DataType.LONG) {      buffer.putInt(obj != null ? ((Number) obj).intValue() : 0);    } else if (_type == DataType.DOUBLE) {      buffer.putDouble(obj != null ? ((Number) obj).doubleValue() : (double) 0);    } else if (_type == DataType.FLOAT) {      buffer.putFloat(obj != null ? ((Number) obj).floatValue() : (float) 0);    } else if (_type == DataType.SHORT_DATE_TIME) {      if (obj instanceof Date) {        // seems access stores dates in the local timezone.  guess you just        // hope you read it in the same timezone in which it was written!        Calendar cal = Calendar.getInstance();        cal.setTime((Date) obj);        long ms = cal.getTimeInMillis();        ms += (long) TimeZone.getDefault().getOffset(ms);        buffer.putDouble((double) ms / MILLISECONDS_PER_DAY +            DAYS_BETWEEN_EPOCH_AND_1900);      } else {        buffer.putDouble(0d);      }    } else if (_type == DataType.BINARY) {      buffer.put((byte[]) obj);    } else if (_type == DataType.TEXT) {      CharSequence text = (CharSequence) obj;      int maxChars = size / 2;      if (text.length() > maxChars) {        text = text.subSequence(0, maxChars);      }      buffer.put(encodeText(text));    } else if (_type == DataType.OLE) {      buffer.put(writeLongValue((byte[]) obj));    } else if (_type == DataType.MEMO) {      buffer.put(writeLongValue(encodeText((CharSequence) obj).array()));    } else {      throw new IOException("Unsupported data type: " + _type);    }    buffer.flip();    return buffer;  }