コード例 #1
0
ファイル: BaseDataBuffer.java プロジェクト: evaleen/nd4j
  /**
   * Create a data buffer from the given length
   *
   * @param buffer
   * @param length
   */
  public BaseDataBuffer(ByteBuffer buffer, int length) {
    allocationMode = Nd4j.alloc;
    this.length = length;
    buffer.order(ByteOrder.nativeOrder());
    if (allocationMode() == AllocationMode.DIRECT) {
      this.wrappedBuffer = buffer;
    } else if (dataType() == Type.INT) {
      intData = new int[length];
      IntBuffer intBuffer = buffer.asIntBuffer();
      for (int i = 0; i < length; i++) {
        intData[i] = intBuffer.get(i);
      }
    } else if (dataType() == Type.DOUBLE) {
      doubleData = new double[length];
      DoubleBuffer doubleBuffer = buffer.asDoubleBuffer();
      for (int i = 0; i < length; i++) {
        doubleData[i] = doubleBuffer.get(i);
      }

    } else if (dataType() == Type.FLOAT) {
      floatData = new float[length];
      FloatBuffer floatBuffer = buffer.asFloatBuffer();
      for (int i = 0; i < length; i++) {
        floatData[i] = floatBuffer.get(i);
      }
    }
  }
コード例 #2
0
ファイル: BaseDataBuffer.java プロジェクト: evaleen/nd4j
  @Override
  public double getDouble(int i) {
    if (doubleData != null) {
      if (i >= doubleData.length) throw new IllegalStateException("Index out of bounds " + i);
      dirty.set(false);
      return doubleData[i];
    } else if (floatData != null) {
      if (i >= floatData.length) throw new IllegalStateException("Index out of bounds " + i);
      dirty.set(false);
      return (double) floatData[i];
    } else if (intData != null) {
      dirty.set(false);
      return (double) intData[i];
    }

    if (dataType() == Type.FLOAT) {
      dirty.set(false);
      return wrappedBuffer.asFloatBuffer().get(i);
    } else if (dataType() == Type.INT) {
      dirty.set(false);
      return wrappedBuffer.asIntBuffer().get(i);
    } else {
      dirty.set(false);
      return wrappedBuffer.asDoubleBuffer().get(i);
    }
  }
コード例 #3
0
  // Creates the ByteBuffer for the ogg tag
  public void create(Tag tag, ByteBuffer buf, List fields, int tagSize, int padding)
      throws UnsupportedEncodingException {
    String vendorString = ((OggTag) tag).getVendor();
    int vendorLength = Utils.getUTF8Bytes(vendorString).length;
    buf.put(
        new byte[] {
          (byte) (vendorLength & 0xFF),
          (byte) ((vendorLength & 0xFF00) >> 8),
          (byte) ((vendorLength & 0xFF0000) >> 16),
          (byte) ((vendorLength & 0xFF000000) >> 24)
        });
    buf.put(Utils.getUTF8Bytes(vendorString));

    // [user comment list length]
    int listLength = fields.size();
    byte[] b = new byte[4];
    b[3] = (byte) ((listLength & 0xFF000000) >> 24);
    b[2] = (byte) ((listLength & 0x00FF0000) >> 16);
    b[1] = (byte) ((listLength & 0x0000FF00) >> 8);
    b[0] = (byte) (listLength & 0x000000FF);
    buf.put(b);

    Iterator it = fields.iterator();
    while (it.hasNext()) {
      buf.put((byte[]) it.next());
    }
  }
コード例 #4
0
ファイル: JavaNewIO.java プロジェクト: juniuszhou/java
  public static void main(String args[]) {
    try {
      aServer asr = new aServer();

      // file channel.
      FileInputStream is = new FileInputStream("");
      is.read();
      FileChannel cha = is.getChannel();
      ByteBuffer bf = ByteBuffer.allocate(1024);
      bf.flip();

      cha.read(bf);

      // Path Paths
      Path pth = Paths.get("", "");

      // Files some static operation.
      Files.newByteChannel(pth);
      Files.copy(pth, pth);
      // file attribute, other different class for dos and posix system.
      BasicFileAttributes bas = Files.readAttributes(pth, BasicFileAttributes.class);
      bas.size();

    } catch (Exception e) {
      System.err.println(e);
    }

    System.out.println("hello ");
  }
コード例 #5
0
ファイル: TCPSock.java プロジェクト: sherwinyu/class
  /**
   * sends data from buffer.
   *
   * <p>precondition: sendbb is in put mode post condition: sendbb is in put mode
   */
  private void sendFromBuffer() {
    aa(this, isSender(), "nonsender can't write");
    aa(this, isConnected() || isClosurePending(), "needs to be established can't write");

    // switch sendbb to get mode
    sendbb.flip();
    p(this, 3, "Sending from buffer. bb flipped.");
    int payloadLength = Math.min(Transport.MAX_PAYLOAD_SIZE, sendbb.limit());
    p(this, 4, "Max Payload size: " + Transport.MAX_PAYLOAD_SIZE);
    p(this, 4, "payloadlen: " + payloadLength);
    dumpState(4);

    if (roomForPacket(payloadLength) && payloadLength > 0) {
      byte[] payload = new byte[payloadLength];
      sendbb.get(payload);

      Transport t = makeTransport(Transport.DATA, seqNum, payload);
      tcpMan.sendData(this.tsid, t);

      p(this, 3, "Write: converting to packet: " + TCPManager.bytesToString(payload));

      // only increment the seqNum if a packet is sent
      seqNum += payloadLength;
    }

    // switch back to put mode
    sendbb.compact();
    dumpState(4);
    if (isClosurePending() && sendbb.position() == 0) release();
  }
コード例 #6
0
 public void serialize(final ByteBuffer buffer) {
   buffer.putShort(this.m_definition.getInstanceId());
   buffer.putShort((short) this.m_results.size());
   for (int i = 0; i < this.m_results.size(); ++i) {
     this.m_results.get(i).serialize(buffer);
   }
 }
コード例 #7
0
  /** {@inheritDoc} */
  @SuppressWarnings("all")
  @Override
  public boolean readFrom(ByteBuffer buf) {
    commState.setBuffer(buf);

    if (!super.readFrom(buf)) return false;

    switch (commState.idx) {
      case 2:
        byte[] entriesBytes0 = commState.getByteArray();

        if (entriesBytes0 == BYTE_ARR_NOT_READ) return false;

        entriesBytes = entriesBytes0;

        commState.idx++;

      case 3:
        if (buf.remaining() < 8) return false;

        futId = commState.getLong();

        commState.idx++;

      case 4:
        if (buf.remaining() < 8) return false;

        topVer = commState.getLong();

        commState.idx++;
    }

    return true;
  }
コード例 #8
0
 @Override
 public boolean serialize(final ByteBuffer buffer) {
   if (this.contents.size() > 65535) {
     return false;
   }
   buffer.putShort((short) this.contents.size());
   for (int i = 0; i < this.contents.size(); ++i) {
     final Content contents_element = this.contents.get(i);
     final boolean contents_element_ok = contents_element.serialize(buffer);
     if (!contents_element_ok) {
       return false;
     }
   }
   if (this.contentsSelection != null) {
     buffer.put((byte) 1);
     final boolean contentsSelection_ok = this.contentsSelection.serialize(buffer);
     if (!contentsSelection_ok) {
       return false;
     }
   } else {
     buffer.put((byte) 0);
   }
   if (this.buyableContents.size() > 65535) {
     return false;
   }
   buffer.putShort((short) this.buyableContents.size());
   for (int i = 0; i < this.buyableContents.size(); ++i) {
     final BuyableContent buyableContents_element = this.buyableContents.get(i);
     final boolean buyableContents_element_ok = buyableContents_element.serialize(buffer);
     if (!buyableContents_element_ok) {
       return false;
     }
   }
   return true;
 }
コード例 #9
0
ファイル: Util.java プロジェクト: saruna/incubator-trafodion
 static byte[] extractByteArray(ByteBuffer buf) {
   int len = buf.getInt();
   if (len > 0) {
     byte[] a = new byte[len];
     buf.get(a, 0, len);
     return a;
   } else return new byte[0];
 }
コード例 #10
0
ファイル: BaseDataBuffer.java プロジェクト: evaleen/nd4j
  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);
    }
  }
コード例 #11
0
ファイル: Client.java プロジェクト: jinxingwang/cse461
 // Takes the byte array received from the server and parses.
 // The server responds with a UDP packet containing 4 integers. The
 // integer array returned contains has those 4 integers.
 public static int[] partA(byte[] response) {
   ByteBuffer bb = ByteBuffer.wrap(response);
   checkHeader(bb, 16, 0, STEP2);
   int[] res = new int[4];
   for (int i = 0; i < res.length; i++) {
     res[i] = bb.getInt();
   }
   return res;
 }
コード例 #12
0
ファイル: NSOutputStream.java プロジェクト: ZeerDonker/robovm
 public NSOutputStream(ByteBuffer bytes) {
   super((SkipInit) null);
   if (bytes == null) {
     throw new NullPointerException("bytes");
   }
   long handle = NSData.getEffectiveAddress(bytes) + bytes.position();
   initObject(init(handle, bytes.remaining()));
   addStrongRef(bytes);
 }
コード例 #13
0
ファイル: Util.java プロジェクト: saruna/incubator-trafodion
 static void printBBInfo(ByteBuffer buf) {
   LOG.debug(
       "Info : position,limit,capacity "
           + buf.position()
           + ","
           + buf.limit()
           + ","
           + buf.capacity());
 }
コード例 #14
0
ファイル: Util.java プロジェクト: saruna/incubator-trafodion
 static void insertString(String str, ByteBuffer buf) throws java.io.UnsupportedEncodingException {
   if (str != null && str.length() > 0) {
     buf.putInt(str.length() + 1);
     buf.put(str.getBytes("UTF-8"), 0, str.length());
     buf.put((byte) 0);
   } else { // buffer is null or length 0
     buf.putInt(0);
   }
 }
コード例 #15
0
  /** {@inheritDoc} */
  @Nullable
  @Override
  public GridClientMessage decode(GridNioSession ses, ByteBuffer buf)
      throws IOException, GridException {
    ParserState state = ses.removeMeta(PARSER_STATE_META_NAME);

    if (state == null) state = new ParserState();

    PacketType type = state.packetType();

    if (type == null) {
      byte hdr = buf.get(buf.position());

      switch (hdr) {
        case MEMCACHE_REQ_FLAG:
          state.packet(new GridTcpRestPacket());
          state.packetType(PacketType.MEMCACHE);

          break;
        case GRIDGAIN_REQ_FLAG:
          // Skip header.
          buf.get();

          state.packetType(PacketType.GRIDGAIN);

          break;

        default:
          throw new IOException(
              "Failed to parse incoming packet (invalid packet start) [ses="
                  + ses
                  + ", b="
                  + Integer.toHexString(hdr & 0xFF)
                  + ']');
      }
    }

    GridClientMessage result = null;

    switch (state.packetType()) {
      case MEMCACHE:
        result = parseMemcachePacket(ses, buf, state);

        break;
      case GRIDGAIN:
        result = parseCustomPacket(ses, buf, state);

        break;
    }

    if (result == null)
      // Packet was not fully parsed yet.
      ses.addMeta(PARSER_STATE_META_NAME, state);

    return result;
  }
コード例 #16
0
ファイル: Util.java プロジェクト: saruna/incubator-trafodion
 static void insertByteString(byte[] array, ByteBuffer buf)
     throws java.io.UnsupportedEncodingException {
   if (array != null && array.length > 0) {
     buf.putInt(array.length);
     buf.put(array, 0, array.length);
     buf.put((byte) 0);
   } else { // buffer is null or length 0
     buf.putInt(0);
   }
 }
コード例 #17
0
ファイル: Util.java プロジェクト: saruna/incubator-trafodion
 static byte[] extractByteString(ByteBuffer buf) {
   int len = buf.getInt();
   byte[] str = null;
   if (len > 0) {
     str = new byte[len];
     buf.get(str, 0, len);
     buf.get(); // trailing null
   } else str = new byte[0];
   return str;
 }
コード例 #18
0
ファイル: BaseDataBuffer.java プロジェクト: joachimdb/nd4j
 @Override
 public IntBuffer asNioInt() {
   if (wrappedBuffer == null) {
     if (offset() > 0) return (IntBuffer) IntBuffer.wrap(intData).position(offset());
     else return IntBuffer.wrap(intData);
   }
   if (offset() == 0) {
     return wrappedBuffer.asIntBuffer();
   } else return (IntBuffer) wrappedBuffer.asIntBuffer().position(offset());
 }
コード例 #19
0
ファイル: TCPSock.java プロジェクト: sherwinyu/class
  /**
   * Read from the socket up to len bytes into the buffer buf starting at position pos.
   *
   * @param buf byte[] the buffer
   * @param pos int starting position in buffer
   * @param len int number of bytes to read
   * @return int on success, the number of bytes read, which may be smaller than len; on failure, -1
   */
  public int read(byte[] buf, int pos, int len) {
    aa(this, isReceiver(), "nonreceiver socket reading");
    aa(this, state == State.ESTABLISHED, "attempting to read from closed socket");

    recvbb.flip();
    int bytesCopied = Math.min(recvbb.limit(), len);
    recvbb.get(buf, pos, bytesCopied);
    recvbb.compact();

    return bytesCopied;
  }
コード例 #20
0
ファイル: BaseDataBuffer.java プロジェクト: joachimdb/nd4j
 @Override
 public FloatBuffer asNioFloat() {
   if (wrappedBuffer == null) {
     if (offset() == 0) {
       return FloatBuffer.wrap(floatData);
     } else return (FloatBuffer) FloatBuffer.wrap(floatData).position(offset());
   }
   if (offset() == 0) {
     return wrappedBuffer.asFloatBuffer();
   } else return (FloatBuffer) wrappedBuffer.asFloatBuffer().position(offset());
 }
コード例 #21
0
ファイル: Util.java プロジェクト: saruna/incubator-trafodion
 static String extractString(ByteBuffer buf) throws java.io.UnsupportedEncodingException {
   int len = buf.getInt();
   byte[] str = null;
   if (len > 0) {
     str = new byte[len - 1];
     ;
     buf.get(str, 0, len - 1);
     buf.get(); // trailing null
   } else str = new byte[0];
   return new String(str, "UTF-8");
 }
コード例 #22
0
ファイル: BaseDataBuffer.java プロジェクト: joachimdb/nd4j
  @Override
  public DoubleBuffer asNioDouble() {
    if (wrappedBuffer == null) {
      if (offset() == 0) {
        return DoubleBuffer.wrap(doubleData);
      } else return (DoubleBuffer) DoubleBuffer.wrap(doubleData).position(offset());
    }

    if (offset() == 0) {
      return wrappedBuffer.asDoubleBuffer();
    } else return (DoubleBuffer) wrappedBuffer.asDoubleBuffer().position(offset());
  }
コード例 #23
0
  /** {@inheritDoc} */
  @SuppressWarnings("all")
  @Override
  public boolean readFrom(ByteBuffer buf) {
    commState.setBuffer(buf);

    if (!super.readFrom(buf)) return false;

    switch (commState.idx) {
      case 2:
        if (buf.remaining() < 1) return false;

        err = commState.getBoolean();

        commState.idx++;

      case 3:
        if (buf.remaining() < 8) return false;

        futId = commState.getLong();

        commState.idx++;

      case 4:
        if (commState.readSize == -1) {
          if (buf.remaining() < 4) return false;

          commState.readSize = commState.getInt();
        }

        if (commState.readSize >= 0) {
          if (rejectedKeyBytes == null) rejectedKeyBytes = new ArrayList<>(commState.readSize);

          for (int i = commState.readItems; i < commState.readSize; i++) {
            byte[] _val = commState.getByteArray();

            if (_val == BYTE_ARR_NOT_READ) return false;

            rejectedKeyBytes.add((byte[]) _val);

            commState.readItems++;
          }
        }

        commState.readSize = -1;
        commState.readItems = 0;

        commState.idx++;
    }

    return true;
  }
コード例 #24
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;
 }
コード例 #25
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;
 }
コード例 #26
0
  void isReadable(SelectionKey k) {
    EventableChannel ec = (EventableChannel) k.attachment();
    long b = ec.getBinding();

    if (ec.isWatchOnly()) {
      if (ec.isNotifyReadable()) eventCallback(b, EM_CONNECTION_NOTIFY_READABLE, null);
    } else {
      myReadBuffer.clear();

      try {
        ec.readInboundData(myReadBuffer);
        myReadBuffer.flip();
        if (myReadBuffer.limit() > 0) {
          if (ProxyConnections != null) {
            EventableChannel target = ProxyConnections.get(b);
            if (target != null) {
              ByteBuffer myWriteBuffer = ByteBuffer.allocate(myReadBuffer.limit());
              myWriteBuffer.put(myReadBuffer);
              myWriteBuffer.flip();
              target.scheduleOutboundData(myWriteBuffer);
            } else {
              eventCallback(b, EM_CONNECTION_READ, myReadBuffer);
            }
          } else {
            eventCallback(b, EM_CONNECTION_READ, myReadBuffer);
          }
        }
      } catch (IOException e) {
        UnboundConnections.add(b);
      }
    }
  }
コード例 #27
0
ファイル: Client.java プロジェクト: jinxingwang/cse461
 public static void checkHeader(ByteBuffer b, int length, int psecret, short step) {
   int l = b.getInt();
   int ps = b.getInt();
   int s = b.getShort();
   b.getShort(); // the student number, which I am not checking.
   if (l != length) {
     System.out.println("Packet length was " + l + " instead of " + length);
   }
   if (ps != psecret) {
     System.out.println("Psecret was " + ps + " instead of " + psecret);
   }
   if (s != step) {
     System.out.println("Packet length was " + s + " instead of " + step);
   }
 }
コード例 #28
0
  /** {@inheritDoc} */
  @SuppressWarnings("all")
  @Override
  public boolean readFrom(ByteBuffer buf) {
    commState.setBuffer(buf);

    if (!super.readFrom(buf)) return false;

    switch (commState.idx) {
      case 2:
        byte[] mapBytes0 = commState.getByteArray();

        if (mapBytes0 == BYTE_ARR_NOT_READ) return false;

        mapBytes = mapBytes0;

        commState.idx++;

      case 3:
        if (buf.remaining() < 1) return false;

        rmvLocks = commState.getBoolean();

        commState.idx++;
    }

    return true;
  }
コード例 #29
0
 public void unserializeResults(final ByteBuffer buffer) {
   final short resultsCount = buffer.getShort();
   for (int i = 0; i < resultsCount; ++i) {
     final DungeonLadderResult result = DungeonLadderResult.fromBuild(buffer);
     this.insertResult(result);
   }
 }
コード例 #30
0
ファイル: TCPSock.java プロジェクト: sherwinyu/class
 private void initSender() {
   aa(this, sockType == SocketType.SENDER, "");
   sendWindow = 3 * Transport.MAX_PAYLOAD_SIZE;
   sendBase = 0;
   seqNum = 0;
   sendbb = ByteBuffer.allocate(SEND_BUFFER_SIZE);
 }