Ejemplo n.º 1
0
  private FPoint2D parsePoint(ByteBuffer data, boolean haveZ, boolean haveM) {
    double X = data.getDouble();
    double Y = data.getDouble();
    FPoint2D result = new FPoint2D(X, Y);

    if ((haveZ) && (haveM)) {
      // TODO: SUPPORT ZM (POR AHORA, LA Z TIENE PREFERENCIA
      double Z = data.getDouble();
      result = new FPoint3D(X, Y, Z);
      double M = data.getDouble();
      // TODO: create future FPoint3DM

      return result;
    }
    if (haveM) {
      double m = data.getDouble();
      result = new FPoint2DM(X, Y, m);
    }

    if (haveZ) {
      double Z = data.getDouble();
      result = new FPoint3D(X, Y, Z);
    }

    return result;
  }
Ejemplo n.º 2
0
 /**
  * Read this vector from the supplied {@link ByteBuffer} starting at the specified absolute buffer
  * position/index.
  *
  * <p>This method will not increment the position of the given ByteBuffer.
  *
  * @param index the absolute position into the ByteBuffer
  * @param buffer values will be read in <tt>x, y, z, w</tt> order
  * @return this
  */
 public Vector4d set(int index, ByteBuffer buffer) {
   x = buffer.getDouble(index);
   y = buffer.getDouble(index + 8);
   z = buffer.getDouble(index + 16);
   w = buffer.getDouble(index + 24);
   return this;
 }
  /**
   * read the Header from the given ByteBuffer.
   *
   * @param bb ByteBuffer.
   * @throws Exception
   */
  public void read(ByteBuffer bb) throws Exception {
    // MAIN FILE HEADER
    bb.order(ByteOrder.BIG_ENDIAN);
    // magic number
    int SHP_MAGIC_read = bb.getInt(0);
    if (SHP_MAGIC_read != SHP_MAGIC) {
      throw new Exception("(ShapeFile) error: SHP_MAGIC = " + SHP_MAGIC + ", File: " + file);
    }
    // file length
    SHP_file_length = bb.getInt(24);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    int SHP_version_read = bb.getInt(28);
    if (SHP_version_read != SHP_VERSION) {
      throw new Exception("(ShapeFile) error: SHP_VERSION = " + SHP_VERSION + ", File: " + file);
    }

    SHP_shape_type = bb.getInt(32);

    try {
      shape_type = ShpShape.Type.byID(SHP_shape_type);
    } catch (Exception e) {
      e.printStackTrace();
    }

    SHP_bbox[0][0] = bb.getDouble(36); // x-min
    SHP_bbox[1][0] = bb.getDouble(44); // y-min
    SHP_bbox[0][1] = bb.getDouble(52); // x-max
    SHP_bbox[1][1] = bb.getDouble(60); // y-max
    SHP_bbox[2][1] = bb.getDouble(68); // z-min
    SHP_bbox[2][1] = bb.getDouble(76); // z-max
    SHP_range_m[0] = bb.getDouble(84); // m-min
    SHP_range_m[1] = bb.getDouble(92); // m-max

    bb.position(100);
  }
 public void readFrom(ByteBuffer buffer) {
   x0 = buffer.getDouble(); // 8 bytes read
   y0 = buffer.getDouble(); // 16 bytes read
   z0 = buffer.getDouble(); // 24 bytes read
   x1 = buffer.getDouble(); // 32 bytes read
   y1 = buffer.getDouble(); // 40 bytes read
   z1 = buffer.getDouble(); // 48 bytes read
 }
Ejemplo n.º 5
0
 public void deserialize(ByteBuffer bb) {
   header.deserialize(bb);
   link_name = Serialization.readString(bb);
   type = bb.getInt();
   orientation.deserialize(bb);
   absolute_roll_tolerance = bb.getDouble();
   absolute_pitch_tolerance = bb.getDouble();
   absolute_yaw_tolerance = bb.getDouble();
   weight = bb.getDouble();
 }
Ejemplo n.º 6
0
    public Object objectFromBuffer(byte[] buffer, int offset, int length) throws Exception {
      ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length);

      byte type = buf.get();
      switch (type) {
        case START:
        case GET_CONFIG:
          return new MethodCall(type);
        case SET_OOB:
        case SET_SYNC:
          return new MethodCall(type, buf.get() == 1);
        case SET_NUM_MSGS:
        case SET_NUM_THREADS:
        case SET_MSG_SIZE:
        case SET_ANYCAST_COUNT:
          return new MethodCall(type, buf.getInt());
        case GET:
          return new MethodCall(type, buf.getLong());
        case PUT:
          Long longarg = buf.getLong();
          int len = buf.getInt();
          byte[] arg2 = new byte[len];
          buf.get(arg2, 0, arg2.length);
          return new MethodCall(type, longarg, arg2);
        case SET_READ_PERCENTAGE:
          return new MethodCall(type, buf.getDouble());
        default:
          throw new IllegalStateException("type " + type + " not known");
      }
    }
Ejemplo n.º 7
0
 public double toBigEndian(double d) {
   ByteBuffer bb = ByteBuffer.wrap(new byte[8]);
   bb.putDouble(d);
   bb.order(ByteOrder.LITTLE_ENDIAN);
   bb.position(0);
   return bb.getDouble();
 }
  protected Double[] readDoubleArray(InputStream in) {
    byte[] data = null;

    try {
      int len = (int) readUnsignedVarint(in);

      data = new byte[len * 8];
      if (in.read(data, 0, len * 8) != len * 8) {
        debug("readDoubleArray: could not read array");
        return null;
      }

      ByteBuffer bb = ByteBuffer.allocate(8 * len);
      bb.put(data);
      bb.flip();
      Double[] res = new Double[len];
      for (int i = 0; i < len; i++) {

        res[i] = bb.getDouble();
      }
      return res;
    } catch (IOException e) {
      debug("readDoubleArray: could not read array");
      e.printStackTrace();
    }
    return null;
  }
Ejemplo n.º 9
0
 public double readDouble() throws IOException {
   try {
     return buf.getDouble();
   } catch (BufferUnderflowException e) {
     throw new EOFException();
   }
 }
Ejemplo n.º 10
0
 private Object getValue(MAVLinkDataType type, int offset) {
   switch (type) {
     case CHAR:
       return payloadBB.get(offset);
     case UINT8:
       return payloadBB.get(offset) & 0xFF;
     case INT8:
       return (int) payloadBB.get(offset);
     case UINT16:
       return payloadBB.getShort(offset) & 0xFFFF;
     case INT16:
       return (int) payloadBB.getShort(offset);
     case UINT32:
       return payloadBB.getInt(offset) & 0xFFFFFFFFl;
     case INT32:
       return payloadBB.getInt(offset);
     case UINT64:
       return payloadBB.getLong(offset);
     case INT64:
       return payloadBB.getLong(offset);
     case FLOAT:
       return payloadBB.getFloat(offset);
     case DOUBLE:
       return payloadBB.getDouble(offset);
     default:
       throw new RuntimeException("Unknown type: " + type);
   }
 }
Ejemplo n.º 11
0
 public static double readReal(FileChannel channel) throws IOException {
   ByteBuffer bb = ByteBuffer.allocate(8);
   bb.order(ByteOrder.LITTLE_ENDIAN);
   channel.read(bb);
   bb.rewind();
   return bb.getDouble();
 }
 public static void main(String[] args) {
   ByteBuffer bb = ByteBuffer.allocate(BSIZE);
   // Allocation automatically zeroes the ByteBuffer:
   int i = 0;
   while (i++ < bb.limit()) if (bb.get() != 0) print("nonzero");
   print("i = " + i);
   bb.rewind();
   // Store and read a char array:
   bb.asCharBuffer().put("Howdy!");
   char c;
   while ((c = bb.getChar()) != 0) printnb(c + " ");
   print();
   bb.rewind();
   // Store and read a short:
   bb.asShortBuffer().put((short) 471142);
   print(bb.getShort());
   bb.rewind();
   // Store and read an int:
   bb.asIntBuffer().put(99471142);
   print(bb.getInt());
   bb.rewind();
   // Store and read a long:
   bb.asLongBuffer().put(99471142);
   print(bb.getLong());
   bb.rewind();
   // Store and read a float:
   bb.asFloatBuffer().put(99471142);
   print(bb.getFloat());
   bb.rewind();
   // Store and read a double:
   bb.asDoubleBuffer().put(99471142);
   print(bb.getDouble());
   bb.rewind();
 }
Ejemplo n.º 13
0
 private Object getSingleValue(ByteBuffer buffer) {
   Object v;
   if (type.equals("float")) {
     v = buffer.getFloat();
   } else if (type.equals("double")) {
     v = buffer.getDouble();
   } else if (type.equals("int8_t") || type.equals("bool")) {
     v = (int) buffer.get();
   } else if (type.equals("uint8_t")) {
     v = buffer.get() & 0xFF;
   } else if (type.equals("int16_t")) {
     v = (int) buffer.getShort();
   } else if (type.equals("uint16_t")) {
     v = buffer.getShort() & 0xFFFF;
   } else if (type.equals("int32_t")) {
     v = buffer.getInt();
   } else if (type.equals("uint32_t")) {
     v = buffer.getInt() & 0xFFFFFFFFl;
   } else if (type.equals("int64_t")) {
     v = buffer.getLong();
   } else if (type.equals("uint64_t")) {
     v = buffer.getLong();
   } else if (type.equals("char")) {
     v = buffer.get();
   } else {
     throw new RuntimeException("Unsupported type: " + type);
   }
   return v;
 }
Ejemplo n.º 14
0
 /**
  * Reads the an object of the given Class from the untagged value contained in the ByteBuffer.
  *
  * @param bb contains the Object
  * @param tag TAG of the Value to be read
  * @return the object
  * @throws JdwpException
  * @throws IOException
  */
 public static Object getUntaggedObj(ByteBuffer bb, byte tag) throws JdwpException, IOException {
   switch (tag) {
     case JdwpConstants.Tag.BYTE:
       return new Byte(bb.get());
     case JdwpConstants.Tag.CHAR:
       return new Character(bb.getChar());
     case JdwpConstants.Tag.FLOAT:
       return new Float(bb.getFloat());
     case JdwpConstants.Tag.DOUBLE:
       return new Double(bb.getDouble());
     case JdwpConstants.Tag.INT:
       return new Integer(bb.getInt());
     case JdwpConstants.Tag.LONG:
       return new Long(bb.getLong());
     case JdwpConstants.Tag.SHORT:
       return new Short(bb.getShort());
     case JdwpConstants.Tag.VOID:
       return new byte[0];
     case JdwpConstants.Tag.BOOLEAN:
       return (bb.get() == 0) ? new Boolean(false) : new Boolean(true);
     case JdwpConstants.Tag.STRING:
       return JdwpString.readString(bb);
     case JdwpConstants.Tag.ARRAY:
     case JdwpConstants.Tag.THREAD:
     case JdwpConstants.Tag.OBJECT:
     case JdwpConstants.Tag.THREAD_GROUP:
     case JdwpConstants.Tag.CLASS_LOADER:
     case JdwpConstants.Tag.CLASS_OBJECT:
       // All these cases are ObjectIds
       ObjectId oid = VMIdManager.getDefault().readObjectId(bb);
       return oid.getObject();
     default:
       throw new NotImplementedException("Tag " + tag + " is not implemented.");
   }
 }
Ejemplo n.º 15
0
  @Theory
  public void shouldPutDoubleToBuffer(final AtomicBuffer buffer) {
    final ByteBuffer duplicateBuffer = byteBuffer(buffer);

    buffer.putDouble(INDEX, DOUBLE_VALUE, BYTE_ORDER);

    assertThat(duplicateBuffer.getDouble(INDEX), is(DOUBLE_VALUE));
  }
Ejemplo n.º 16
0
 public CAFDesc(ByteBuffer buf) {
   sampleRate = buf.getDouble();
   formatID = getString(buf, 4);
   formatFlags = buf.getInt();
   bytesPerPacket = buf.getInt();
   framesPerPacket = buf.getInt();
   channelsPerFrame = buf.getInt();
 }
Ejemplo n.º 17
0
 @Override
 public void deserializeUnlocked(ByteBuffer data) {
   this.params.clear();
   int size = data.getInt();
   for (int i = 0; i < size; i++) {
     this.params.put(data.getInt(), data.getDouble());
   }
 }
Ejemplo n.º 18
0
 /**
  * Read double from this packet buffer.
  *
  * @return double
  */
 protected final double readDF() {
   try {
     return buf.getDouble();
   } catch (Exception e) {
     log.error("Missing DF for: " + this);
   }
   return 0;
 }
Ejemplo n.º 19
0
  @Theory
  public void shouldPutDoubleToNativeBuffer(final AtomicBuffer buffer) {
    final ByteBuffer duplicateBuffer = byteBuffer(buffer);
    duplicateBuffer.order(ByteOrder.nativeOrder());

    buffer.putDouble(INDEX, DOUBLE_VALUE);

    assertThat(duplicateBuffer.getDouble(INDEX), is(DOUBLE_VALUE));
  }
 /**
  * Populates the strategy from the byte array content. The structure should be as follows:
  * [profitShare][profitsLagId][receiversId][depositId][reservesId]
  *
  * @param content the byte array containing the structure of the strategy
  * @param pop the Macro Population of agents
  */
 @Override
 public void populateFromBytes(byte[] content, MacroPopulation pop) {
   ByteBuffer buf = ByteBuffer.wrap(content);
   this.profitShare = buf.getDouble();
   this.profitsLagId = buf.getInt();
   this.receiversId = buf.getInt();
   this.depositId = buf.getInt();
   this.reservesId = buf.getInt();
 }
Ejemplo n.º 21
0
 public double getDouble() {
   double v;
   if (this.awxEndian == EndianByteBuffer.LITTLE_ENDIAN) {
     v = byteBuffer.order(ByteOrder.LITTLE_ENDIAN).getDouble();
   } else {
     v = byteBuffer.getDouble();
   }
   return v;
 }
 @Override
 public Object read(ByteBuffer buff, int tag) {
   if (tag != TYPE_ARRAY) {
     byte[] data;
     int len = tag - TAG_BYTE_ARRAY_0_15;
     data = DataUtils.newBytes(len);
     buff.get(data);
     return data;
   }
   int ct = buff.get();
   Class<?> clazz;
   Object obj;
   if (ct == -1) {
     String componentType = StringDataType.INSTANCE.read(buff);
     try {
       clazz = Class.forName(componentType);
     } catch (Exception e) {
       throw DataUtils.newIllegalStateException("Could not get class {0}", componentType, e);
     }
   } else {
     clazz = COMMON_CLASSES[ct];
   }
   int len = DataUtils.readVarInt(buff);
   try {
     obj = Array.newInstance(clazz, len);
   } catch (Exception e) {
     throw DataUtils.newIllegalStateException(
         "Could not create array of type {0} length {1}", clazz, len, e);
   }
   if (clazz.isPrimitive()) {
     for (int i = 0; i < len; i++) {
       if (clazz == boolean.class) {
         ((boolean[]) obj)[i] = buff.get() == 1;
       } else if (clazz == byte.class) {
         ((byte[]) obj)[i] = buff.get();
       } else if (clazz == char.class) {
         ((char[]) obj)[i] = buff.getChar();
       } else if (clazz == short.class) {
         ((short[]) obj)[i] = buff.getShort();
       } else if (clazz == int.class) {
         ((int[]) obj)[i] = buff.getInt();
       } else if (clazz == float.class) {
         ((float[]) obj)[i] = buff.getFloat();
       } else if (clazz == double.class) {
         ((double[]) obj)[i] = buff.getDouble();
       } else {
         ((long[]) obj)[i] = buff.getLong();
       }
     }
   } else {
     Object[] array = (Object[]) obj;
     for (int i = 0; i < len; i++) {
       array[i] = elementType.read(buff);
     }
   }
   return obj;
 }
Ejemplo n.º 23
0
 private Number fractional() {
   switch (size) {
     case 4:
       return byteBuffer.getFloat();
     case 8:
       return byteBuffer.getDouble();
     default:
       throw new UnsupportedOperationException("invalid bytes size for fractional number");
   }
 }
Ejemplo n.º 24
0
 public WindowInputHandler setWindow(Window window) {
   if (this.window != null) {
     this.window.setKeyCallback(null);
   }
   this.window = window;
   if (window != null) {
     ByteBuffer buf = BufferUtils.createByteBuffer(16);
     buf.limit(8);
     ByteBuffer bufX = buf.slice();
     buf.limit(16);
     buf.position(8);
     ByteBuffer bufY = buf.slice();
     window.getCursorPos(bufX.asDoubleBuffer(), bufY.asDoubleBuffer());
     mouseX = bufX.getDouble();
     mouseY = window.getViewport().getHeight() - bufY.getDouble();
     setImmediateMode(immediateMode);
   }
   return this;
 }
Ejemplo n.º 25
0
 public static double getMean(ByteBuffer simulationResults) {
   double sum = 0.0;
   int size = simulationResults.capacity() / 8;
   simulationResults.rewind();
   for (int i = 0; i < size; i++) {
     double f = simulationResults.getDouble();
     sum += f;
   }
   return (sum / size);
 }
Ejemplo n.º 26
0
 public static double getVariance(ByteBuffer simulationResults) {
   double mean = getMean(simulationResults);
   double temp = 0.0;
   int size = simulationResults.capacity() / 8;
   simulationResults.rewind();
   for (int i = 0; i < size; i++) {
     double f = simulationResults.getDouble();
     temp += (mean - f) * (mean - f);
   }
   return (temp / size);
 }
 /**
  * Receive as many items as possible from the given byte buffer to this buffer.
  *
  * <p>The <TT>receiveItems()</TT> method must not block the calling thread; if it does, all
  * message I/O in MP will be blocked.
  *
  * @param i Index of first item to receive, in the range 0 .. <TT>length</TT>-1.
  * @param num Maximum number of items to receive.
  * @param buffer Byte buffer.
  * @return Number of items received.
  */
 protected int receiveItems(int i, int num, ByteBuffer buffer) {
   int index = i;
   int off = myArrayOffset + i;
   int max = Math.min(i + num, myLength);
   while (index < max && buffer.remaining() >= 8) {
     myArray.reduce(off, buffer.getDouble(), myOp);
     ++index;
     ++off;
   }
   return index - i;
 }
Ejemplo n.º 28
0
 /* @see java.io.DataInput.readDouble() */
 public double readDouble() throws IOException {
   buffer(position, 8);
   position += 8;
   try {
     return buffer.getDouble();
   } catch (BufferUnderflowException e) {
     EOFException eof = new EOFException(EOF_ERROR_MSG);
     eof.initCause(e);
     throw eof;
   }
 }
Ejemplo n.º 29
0
  /**
   * Transfer (by bytes) the data at the current record to the ShapefileWriter.
   *
   * @param bounds double array of length four for transfering the bounds into
   * @return The length of the record transfered in bytes
   */
  public int transferTo(ShapefileWriter writer, int recordNum, double[] bounds) throws IOException {

    buffer.position(this.toBufferOffset(record.end));
    buffer.order(ByteOrder.BIG_ENDIAN);

    buffer.getInt(); // record number
    int rl = buffer.getInt();
    int mark = buffer.position();
    int len = rl * 2;

    buffer.order(ByteOrder.LITTLE_ENDIAN);
    ShapeType recordType = ShapeType.forID(buffer.getInt());

    if (recordType.isMultiPoint()) {
      for (int i = 0; i < 4; i++) {
        bounds[i] = buffer.getDouble();
      }
    } else if (recordType != ShapeType.NULL) {
      bounds[0] = bounds[1] = buffer.getDouble();
      bounds[2] = bounds[3] = buffer.getDouble();
    }

    // write header to shp and shx
    headerTransfer.position(0);
    headerTransfer.putInt(recordNum).putInt(rl).position(0);
    writer.shpChannel.write(headerTransfer);
    headerTransfer.putInt(0, writer.offset).position(0);
    writer.offset += rl + 4;
    writer.shxChannel.write(headerTransfer);

    // reset to mark and limit at end of record, then write
    int oldLimit = buffer.limit();
    buffer.position(mark).limit(mark + len);
    writer.shpChannel.write(buffer);
    buffer.limit(oldLimit);

    record.end = this.toFileOffset(buffer.position());
    record.number++;

    return len;
  }
 @Override
 public Object read(ByteBuffer buff, int tag) {
   switch (tag) {
     case TAG_DOUBLE_0:
       return 0d;
     case TAG_DOUBLE_1:
       return 1d;
     case TAG_DOUBLE_FIXED:
       return buff.getDouble();
   }
   return Double.longBitsToDouble(Long.reverse(DataUtils.readVarLong(buff)));
 }