public static object GetObject(int type, int numel, ByteBuffer buf) { switch (type) { case CHAR: byte[] strBytes = new byte[numel]; buf.Get(ref strBytes); System.Text.Encoding encoding = buf.Encoding; string val = encoding.GetString(strBytes, 0, strBytes.Length); return val; case INT8: goto case UINT8; case UINT8: byte[] int8array = new byte[numel]; buf.Get(ref int8array); return (object)int8array; case INT16: goto case UINT16; case UINT16: short[] int16array = new short[numel]; // The following would be faster, but DOES NOT // increment the position of the original ByteBuffer!!! // buf.asShortBuffer().get(int16array); for (int i = 0; i < numel; i++) int16array[i] = buf.GetShort(); return (object)int16array; case INT32: goto case UINT32; case UINT32: int[] int32array = new int[numel]; for (int i = 0; i < numel; i++) int32array[i] = buf.GetInt(); return (object)int32array; case INT64: goto case UINT64; case UINT64: long[] int64array = new long[numel]; for (int i = 0; i < numel; i++) int64array[i] = buf.GetLong(); return (object)int64array; case FLOAT32: float[] float32array = new float[numel]; for (int i = 0; i < numel; i++) float32array[i] = buf.GetFloat(); return (object)float32array; case FLOAT64: double[] float64array = new double[numel]; for (int i = 0; i < numel; i++) float64array[i] = buf.GetDouble(); return (object)float64array; default: return null; } }
/// <summary> /// Initializes a Header from the specified <see cref="FieldTrip.Buffer.ByteBuffer"/>. /// </summary> /// <param name="buf">The buffer to read the header from.</param> public Header(ByteBuffer buf) { NumChans = buf.GetInt(); NumSamples = buf.GetInt(); NumEvents = buf.GetInt(); FSample = buf.GetFloat(); DataType = buf.GetInt(); int size = buf.GetInt(); Labels = new string[NumChans]; while (size > 0) { int chunkType = buf.GetInt(); int chunkSize = buf.GetInt(); byte[] bs = new byte[chunkSize]; buf.Get(ref bs); if (chunkType == CHUNK_CHANNEL_NAMES) { int n = 0, len = 0, index = 0; for (int pos = 0; pos < chunkSize; pos++) { if (bs[pos] == 0) { if (len > 0) { Labels[n] = Encoding.Default.GetString(bs, index, len); index = pos + 1; } len = 0; if (++n == NumChans) break; } else { len++; } } } else { // ignore all other chunks for now } size -= 8 + chunkSize; } }