Beispiel #1
0
 static boolean check(CharsetDecoder dec, byte[] bytes, boolean direct, int[] flow) {
   int inPos = flow[0];
   int inLen = flow[1];
   int outPos = flow[2];
   int outLen = flow[3];
   int expedInPos = flow[4];
   int expedOutPos = flow[5];
   CoderResult expedCR = (flow[6] == 0) ? CoderResult.UNDERFLOW : CoderResult.OVERFLOW;
   ByteBuffer bbf;
   CharBuffer cbf;
   if (direct) {
     bbf = ByteBuffer.allocateDirect(inPos + bytes.length);
     cbf = ByteBuffer.allocateDirect((outPos + outLen) * 2).asCharBuffer();
   } else {
     bbf = ByteBuffer.allocate(inPos + bytes.length);
     cbf = CharBuffer.allocate(outPos + outLen);
   }
   bbf.position(inPos);
   bbf.put(bytes).flip().position(inPos).limit(inPos + inLen);
   cbf.position(outPos);
   dec.reset();
   CoderResult cr = dec.decode(bbf, cbf, false);
   if (cr != expedCR || bbf.position() != expedInPos || cbf.position() != expedOutPos) {
     System.out.printf("Expected(direct=%5b): [", direct);
     for (int i : flow) System.out.print(" " + i);
     System.out.println(
         "]  CR=" + cr + ", inPos=" + bbf.position() + ", outPos=" + cbf.position());
     return false;
   }
   return true;
 }
Beispiel #2
0
 static byte[] encode(char[] cc, Charset cs, boolean testDirect, Time t) throws Exception {
   ByteBuffer bbf;
   CharBuffer cbf;
   CharsetEncoder enc = cs.newEncoder();
   String csn = cs.name();
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(cc.length * 4);
     cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
     cbf.put(cc).flip();
   } else {
     bbf = ByteBuffer.allocate(cc.length * 4);
     cbf = CharBuffer.wrap(cc);
   }
   CoderResult cr = null;
   long t1 = System.nanoTime() / 1000;
   for (int i = 0; i < iteration; i++) {
     cbf.rewind();
     bbf.clear();
     enc.reset();
     cr = enc.encode(cbf, bbf, true);
   }
   long t2 = System.nanoTime() / 1000;
   t.t = (t2 - t1) / iteration;
   if (cr != CoderResult.UNDERFLOW) {
     System.out.println("ENC-----------------");
     int pos = cbf.position();
     System.out.printf("  cr=%s, cbf.pos=%d, cc[pos]=%x%n", cr.toString(), pos, cc[pos] & 0xffff);
     throw new RuntimeException("Encoding err: " + csn);
   }
   byte[] bb = new byte[bbf.position()];
   bbf.flip();
   bbf.get(bb);
   return bb;
 }
Beispiel #3
0
  protected void read(DataInputStream s) {
    try {
      ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue());
      referencing = Collections.synchronizedSet(new HashSet<String>());
      dirty = new AtomicBoolean(false);
      allocationMode = AllocationMode.valueOf(s.readUTF());
      length = s.readInt();
      Type t = Type.valueOf(s.readUTF());
      if (t == Type.DOUBLE) {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // double -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // double -> double
            doubleData = new double[length()];
          } else {
            // double -> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        }
      } else {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // float -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // float -> double
            doubleData = new double[length()];
          } else {
            // float-> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Beispiel #4
0
 static char[] decode(byte[] bb, Charset cs, boolean testDirect, Time t) throws Exception {
   String csn = cs.name();
   CharsetDecoder dec = cs.newDecoder();
   ByteBuffer bbf;
   CharBuffer cbf;
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(bb.length);
     cbf = ByteBuffer.allocateDirect(bb.length * 2).asCharBuffer();
     bbf.put(bb);
   } else {
     bbf = ByteBuffer.wrap(bb);
     cbf = CharBuffer.allocate(bb.length);
   }
   CoderResult cr = null;
   long t1 = System.nanoTime() / 1000;
   for (int i = 0; i < iteration; i++) {
     bbf.rewind();
     cbf.clear();
     dec.reset();
     cr = dec.decode(bbf, cbf, true);
   }
   long t2 = System.nanoTime() / 1000;
   t.t = (t2 - t1) / iteration;
   if (cr != CoderResult.UNDERFLOW) {
     System.out.println("DEC-----------------");
     int pos = bbf.position();
     System.out.printf(
         "  cr=%s, bbf.pos=%d, bb[pos]=%x,%x,%x,%x%n",
         cr.toString(),
         pos,
         bb[pos++] & 0xff,
         bb[pos++] & 0xff,
         bb[pos++] & 0xff,
         bb[pos++] & 0xff);
     throw new RuntimeException("Decoding err: " + csn);
   }
   char[] cc = new char[cbf.position()];
   cbf.flip();
   cbf.get(cc);
   return cc;
 }
Beispiel #5
0
 static CoderResult decodeCR(byte[] bb, Charset cs, boolean testDirect) throws Exception {
   CharsetDecoder dec = cs.newDecoder();
   ByteBuffer bbf;
   CharBuffer cbf;
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(bb.length);
     cbf = ByteBuffer.allocateDirect(bb.length * 2).asCharBuffer();
     bbf.put(bb).flip();
   } else {
     bbf = ByteBuffer.wrap(bb);
     cbf = CharBuffer.allocate(bb.length);
   }
   CoderResult cr = null;
   for (int i = 0; i < iteration; i++) {
     bbf.rewind();
     cbf.clear();
     dec.reset();
     cr = dec.decode(bbf, cbf, true);
   }
   return cr;
 }
Beispiel #6
0
 static CoderResult encodeCR(char[] cc, Charset cs, boolean testDirect) throws Exception {
   ByteBuffer bbf;
   CharBuffer cbf;
   CharsetEncoder enc = cs.newEncoder();
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(cc.length * 4);
     cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
     cbf.put(cc).flip();
   } else {
     bbf = ByteBuffer.allocate(cc.length * 4);
     cbf = CharBuffer.wrap(cc);
   }
   CoderResult cr = null;
   for (int i = 0; i < iteration; i++) {
     cbf.rewind();
     bbf.clear();
     enc.reset();
     cr = enc.encode(cbf, bbf, true);
   }
   return cr;
 }
Beispiel #7
0
  /**
   * @param data
   * @param copy
   */
  public BaseDataBuffer(int[] data, boolean copy) {
    allocationMode = Nd4j.alloc;
    if (allocationMode == AllocationMode.HEAP) {
      if (copy) intData = ArrayUtil.copy(data);
      else this.intData = data;

    } else {
      wrappedBuffer = ByteBuffer.allocateDirect(4 * data.length);
      wrappedBuffer.order(ByteOrder.nativeOrder());
      IntBuffer buffer = wrappedBuffer.asIntBuffer();
      for (int i = 0; i < data.length; i++) {
        buffer.put(i, data[i]);
      }
    }
    length = data.length;
  }
Beispiel #8
0
  /**
   * Instantiate a buffer with the given length
   *
   * @param length the length of the buffer
   */
  protected BaseDataBuffer(int length) {
    this.length = length;
    allocationMode = Nd4j.alloc;
    if (length < 0) throw new IllegalArgumentException("Unable to create a buffer of length <= 0");

    ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue());
    if (allocationMode == AllocationMode.HEAP) {
      if (length >= Integer.MAX_VALUE)
        throw new IllegalArgumentException(
            "Length of data buffer can not be > Integer.MAX_VALUE for heap (array based storage) allocation");
      if (dataType() == Type.DOUBLE) doubleData = new double[length];
      else if (dataType() == Type.FLOAT) floatData = new float[length];
    } else {
      if (length * getElementSize() < 0)
        throw new IllegalArgumentException(
            "Unable to create buffer of length " + length + " due to negative length specified");
      wrappedBuffer =
          ByteBuffer.allocateDirect(getElementSize() * length).order(ByteOrder.nativeOrder());
    }
  }
Beispiel #9
0
  /**
   * @param data
   * @param copy
   */
  public BaseDataBuffer(double[] data, boolean copy) {
    allocationMode = Nd4j.alloc;
    if (allocationMode == AllocationMode.HEAP) {
      if (copy) {
        doubleData = ArrayUtil.copy(data);
      } else {
        this.doubleData = data;
      }
    } else {
      wrappedBuffer = ByteBuffer.allocateDirect(8 * data.length);
      wrappedBuffer.order(ByteOrder.nativeOrder());
      DoubleBuffer buffer = wrappedBuffer.asDoubleBuffer();
      for (int i = 0; i < data.length; i++) {
        buffer.put(i, data[i]);
      }
    }

    length = data.length;
    underlyingLength = data.length;
  }
Beispiel #10
0
 static class ClientInfo {
   ByteBuffer inBuf = ByteBuffer.allocateDirect(512);
   ByteBuffer outBuf = ByteBuffer.allocateDirect(512);
   boolean outputPending = false;
   SocketChannel channel;
 }
 protected static CharBuffer newCharBuffer(int numElements) {
   ByteBuffer bb = ByteBuffer.allocateDirect((Character.SIZE / 8) * numElements);
   bb.order(ByteOrder.nativeOrder());
   return bb.asCharBuffer();
 }
 private ByteEncoder() {
   _buf = ByteBuffer.allocateDirect(MAX_OBJECT_SIZE + 2048);
   _buf.order(Bytes.ORDER);
 }
Beispiel #13
0
 // sets the nio wrapped buffer (allows to be overridden for other use cases like cuda)
 protected void setNioBuffer() {
   wrappedBuffer = ByteBuffer.allocateDirect(elementSize * length);
   wrappedBuffer.order(ByteOrder.nativeOrder());
 }
Beispiel #14
0
  @SuppressWarnings("unchecked")
  public Mesh(LinkedHashMap mesh, User user) {

    this.mesh = mesh;
    this.user = user;

    try {
      title = getStringFromHash(mesh, "title", "Some Object");

      ArrayList<Float> vs = new ArrayList<Float>(256);
      ArrayList<Float> ns = new ArrayList<Float>(256);
      ArrayList<Float> tp = new ArrayList<Float>(256);

      LinkedList verts = getListFromHash(mesh, "vertices");
      for (Object vert : verts) {
        vs.add(getFloatFromList(vert, 0, 0f));
        vs.add(getFloatFromList(vert, 1, 0f));
        vs.add(getFloatFromList(vert, 2, 0f));
      }
      LinkedList norms = getListFromHash(mesh, "normals");
      for (Object norm : norms) {
        ns.add(getFloatFromList(norm, 0, 0f));
        ns.add(getFloatFromList(norm, 1, 0f));
        ns.add(getFloatFromList(norm, 2, 0f));
      }
      LinkedList texts = getListFromHash(mesh, "texturepoints");
      for (Object text : texts) {
        tp.add(getFloatFromList(text, 0, 0f));
        tp.add(getFloatFromList(text, 1, 0f));
      }
      ArrayList<Float> vnt = new ArrayList<Float>(1024);
      ArrayList<Short> ind = new ArrayList<Short>(1024);
      short index = 0;
      LinkedList faces = getListFromHash(mesh, "faces");
      for (Object face : faces) {
        for (int i = 0; i < 3; i++) {
          String f = getStringFromList(face, i, "1/1/2");
          StringTokenizer st = new StringTokenizer(f, "/");
          int fv = Integer.parseInt(st.nextToken()) - 1;
          int ft = Integer.parseInt(st.nextToken()) - 1;
          int fn = Integer.parseInt(st.nextToken()) - 1;

          vnt.add(vs.get(fv * 3));
          vnt.add(vs.get(fv * 3 + 1));
          vnt.add(vs.get(fv * 3 + 2));

          vnt.add(ns.get(fn * 3));
          vnt.add(ns.get(fn * 3 + 1));
          vnt.add(ns.get(fn * 3 + 2));

          vnt.add(tp.get(ft * 2));
          vnt.add(tp.get(ft * 2 + 1));

          ind.add(index++);
        }
      }
      vnt.trimToSize();
      float[] va = new float[vnt.size()];
      for (int i = 0; i < vnt.size(); i++) va[i] = vnt.get(i);

      ind.trimToSize();
      short[] ia = new short[ind.size()];
      for (int i = 0; i < ind.size(); i++) ia[i] = ind.get(i);

      vb = ByteBuffer.allocateDirect(va.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
      vb.put(va).position(0);

      ib = ByteBuffer.allocateDirect(ia.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
      ib.put(ia).position(0);

      il = ia.length;

      textures = getListFromHash(mesh, "textures");
      if (textures.size() == 0) textures = list("placeholder");
      vertexShader = user.shaders.get(getStringFromHash(mesh, "vertex-shader", ""));
      fragmentShader = user.shaders.get(getStringFromHash(mesh, "fragment-shader", ""));
      subObjects = getListFromHash(mesh, "sub-items");
      rotationX = getFloatFromList(getListFromHash(mesh, "rotation"), 0, 0f);
      rotationY = getFloatFromList(getListFromHash(mesh, "rotation"), 1, 0f);
      rotationZ = getFloatFromList(getListFromHash(mesh, "rotation"), 2, 0f);
      scaleX = getFloatFromList(getListFromHash(mesh, "scale"), 0, 1f);
      scaleY = getFloatFromList(getListFromHash(mesh, "scale"), 1, 1f);
      scaleZ = getFloatFromList(getListFromHash(mesh, "scale"), 2, 1f);
      lightR = getFloatFromList(getListFromHash(mesh, "light"), 0, 0f);
      lightG = getFloatFromList(getListFromHash(mesh, "light"), 1, 0f);
      lightB = getFloatFromList(getListFromHash(mesh, "light"), 2, 0f);

    } catch (Exception e) {
      e.printStackTrace();
      Log.e("Mesh Constructor", e.getLocalizedMessage());
      return;
    }
  }
Beispiel #15
0
  public void testByteBufferCoder() {
    Persistent3 three =
        new Persistent3(
            10,
            (float) 4.444,
            555555555555L,
            true,
            "this is the fourth persistent string",
            new Date(),
            new byte[] {10, 20, 30, 40, 50, 60, 70, 80, 90, 100},
            null);

    Persistent2 two =
        new Persistent2(
            1,
            (float) 2.222,
            333333333333L,
            false,
            "this is the first persistent string",
            new Date(),
            new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
            three,
            4,
            "this is the second persistent string",
            (ByteBuffer) ByteBuffer.allocateDirect(17).putInt(1234).rewind());

    Persistent1 one =
        new Persistent1(
            2,
            (float) 3.333,
            4444444444444L,
            true,
            "this is the third persistent string",
            new Date(),
            new byte[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
            two);

    ByteBuffer buffer = ByteBuffer.allocateDirect(8 * 1024);
    ByteBufferCoder encoder = new ByteBufferCoder(buffer, true);

    encoder.encodeCodable(one);
    buffer.rewind();

    ByteBufferCoder decoder = new ByteBufferCoder(buffer, true, new Delegate());
    Persistent1 anotherOne = (Persistent1) decoder.decodeCodable();

    if (!one.equals(anotherOne)) {
      fail("unarchived object with type checking does not equal archived one");
    }

    buffer.clear();
    encoder = new ByteBufferCoder(buffer, false);

    encoder.encodeCodable(one);
    buffer.rewind();

    decoder = new ByteBufferCoder(buffer, false, new Delegate());
    anotherOne = (Persistent1) decoder.decodeCodable();

    if (!one.equals(anotherOne)) {
      fail("unarchived object without type checking does not equal archived one");
    }
  }