fieldBytes method

Future<List<int>> fieldBytes(
  1. Object obj,
  2. int col
)

Called to convert the given object to bytes.

@param obj The value to convert; never null. @param col The column this object will be encoded into. @return The bytes of a string representation of the given object in the current character encoding. @throws UnsupportedEncodingException Thrown if the current charset is unsupported.

Implementation

Future<List<int>> fieldBytes(Object obj, final int col) async {
  String? o;
  final int fieldLen = header.getFieldLength(col);
  var fieldType = header.getFieldType(col);
  switch (String.fromCharCode(fieldType)) {
    case 'C':
    case 'c':
      o = await formatter.getFieldString(fieldLen, obj.toString());
      break;
    case 'L':
    case 'l':
      if (obj is bool) {
        o = obj ? 'T' : 'F';
      } else {
        o = '?';
      }
      break;
    case 'M':
    case 'G':
      o = await formatter.getFieldString(fieldLen, obj.toString());
      break;
    case 'N':
    case 'n':
      // int?
      if (header.getFieldDecimalCount(col) == 0) {
        o = formatter.getFieldStringWithDec(fieldLen, 0, obj as num);
        break;
      }
      // else fall into float mode
      o = formatter.getFieldStringWithDec(
          fieldLen, header.getFieldDecimalCount(col), obj as num);
      break;
    case 'F':
    case 'f':
      o = formatter.getFieldStringWithDec(
          fieldLen, header.getFieldDecimalCount(col), obj as num);
      break;
    case 'D':
    case 'd':
      if (obj is DateTime) {
        o = formatter.getFieldStringDate(obj);
      }
      break;
    case '@':
      o = formatter.getFieldStringDate(obj as DateTime);
      // TODO check back on this
      // if (bool.getbool("org.geotools.shapefile.datetime")) {
      //     // Adding the charset to getBytes causes the output to
      //     // get altered for the '@: Timestamp' field.
      //     // And using String.getBytes returns a different array
      //     // in 64-bit platforms so we get chars and cast to byte
      //     // one element at a time.
      //     char[] carr = o.toCharArray();
      //     byte[] barr = new byte[carr.length];
      //     for (int i = 0; i < carr.length; i++) {
      //         barr[i] = (byte) carr[i];
      //     }
      //     return barr;
      // }
      break;
    default:
      throw ArgumentError(
          'Unknown type ' + header.getFieldType(col).toString());
  }

  // convert the string to bytes with the given charset.
  return await charset!.encode(o!);
}