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; } }
protected ByteBuffer readResponse(int expected) { ByteBuffer def = ByteBuffer.allocate(8); def.order(myOrder); readAll(def); def.rewind(); if (def.length() == 0) { errorReturned = BUFFER_READ_ERROR; throw new IOException("Error reading from the buffer server."); } short version = def.getShort(); short _expected = def.getShort(); if (version != VERSION) { errorReturned = VERSION_ERROR; throw new IOException("Invalid VERSION returned."); } else if (_expected != expected) { errorReturned = BUFFER_READ_ERROR; //throw new IOException("Error returned from FieldTrip buffer server."); } else { errorReturned = NO_ERROR; } int size = def.getInt(); ByteBuffer buf = ByteBuffer.allocate(size); buf.order(myOrder); readAll(buf); buf.rewind(); return(buf); }
public Header(ByteBuffer buf) { nChans = buf.getInt(); nSamples = buf.getInt(); nEvents = buf.getInt(); fSample = buf.getFloat(); dataType = buf.getInt(); int size = buf.getInt(); labels = new string[nChans]; 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] =System.Text.Encoding.Default.GetString(bs, index, len); index = pos +1; } len = 0; if (++n == nChans) break; } else { len++; } } } else { // ignore all other chunks for now } size -= 8 + chunkSize; } }
public static int count(ByteBuffer buf) { int num = 0; long pos = buf.position(); while (buf.remaining() >= 32) { int typeType = buf.getInt(); int typeNumEl = buf.getInt(); int valueType = buf.getInt(); int valueNumEl = buf.getInt(); buf.getInt(); // sample buf.getInt(); // offset buf.getInt(); // duration int size = buf.getInt(); int sizeType = typeNumEl * DataType.wordSize[typeType]; int sizeValue = valueNumEl * DataType.wordSize[valueType]; if (sizeType < 0 || sizeValue < 0 || sizeType + sizeValue > size) { return -(1+num); } buf.position(buf.position() + size); num++; } buf.position(pos); return num; }
public BufferEvent(ByteBuffer buf) { wType = new WrappedObject(); wValue = new WrappedObject(); wType.type = buf.getInt(); wType.numel = buf.getInt(); wValue.type = buf.getInt(); wValue.numel = buf.getInt(); sample = buf.getInt(); offset = buf.getInt(); duration = buf.getInt(); int size = buf.getInt(); wType.array = DataType.getObject(wType.type, wType.numel, buf); if (wType.array != null) { wType.size = wType.numel * DataType.wordSize[wType.type]; } wValue.array = DataType.getObject(wValue.type, wValue.numel, buf); if (wValue.array != null) { wValue.size = wValue.numel * DataType.wordSize[wValue.type]; } size -= wType.size + wValue.size; if (size != 0) { buf.position(buf.position() + size); } }
public double[,] getDoubleData(int first, int last) { DataDescription dd = new DataDescription(); ByteBuffer buf = getRawData(first, last, dd); int nSamples = dd.nSamples; int nChans = dd.nChans; double[,] data = new double[nSamples, nChans]; switch (dd.dataType) { case DataType.INT8: for (int i = 0; i < nSamples; i++) { //data[i] = new double[nChans]; for (int j = 0; j < nChans; j++) { data[i, j] = (double)buf.get(); } } break; case DataType.INT16: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (double)buf.getShort(); } } break; case DataType.INT32: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (double)buf.getInt(); } } break; case DataType.INT64: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (double)buf.getLong(); } } break; case DataType.FLOAT32: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = buf.getFloat(); } } break; case DataType.FLOAT64: DoubleBuffer dBuf = buf.asDoubleBuffer(); double[] rowData; for (int n = 0; n < nSamples; n++) { rowData = getRow <double>(data, n); dBuf.get(rowData); } break; default: throw new IOException("Not supported yet - returning zeros."); break; } return(data); }
public float[,] getFloatData(int first, int last) { DataDescription dd = new DataDescription(); ByteBuffer buf = getRawData(first, last, dd); int nSamples = dd.nSamples; int nChans = dd.nChans; float[,] data = new float[nSamples, nChans]; switch (dd.dataType) { case DataType.INT8: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (float)buf.get(); } } break; case DataType.INT16: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (float)buf.getShort(); } } break; case DataType.INT32: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (float)buf.getInt(); } } break; case DataType.FLOAT32: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (float)buf.getFloat(); } } break; case DataType.FLOAT64: for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nChans; j++) { data[i, j] = (float)buf.getDouble(); } } break; default: throw new IOException("Not supported yet - returning zeros."); break; } return(data); }
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); } }