Package java.nio

Examples of java.nio.ByteBuffer


        public void receive(Event evt) {
            if(evt.getType() == Event.MSG) {
                if(exception != null)
                return;
                Message msg=(Message)evt.getArg();
                ByteBuffer buf=ByteBuffer.wrap(msg.getRawBuffer());
                int seqno=buf.getInt();
                if(seqno != next) {
                    exception=new Exception("expected seqno was " + next + ", but received " + seqno);
                    return;
                }
                next++;
View Full Code Here


            this.bytes2=bytes2.buffer;
        }
       
        @Override
        public ByteBufferStreamable call() throws Exception {
            ByteBuffer results = ByteBuffer.allocate(bytes1.remaining() +
                bytes2.remaining());
            int i = bytes1.position();
            int j = bytes2.position();
            byte[] byteArray1 = bytes1.array();
            byte[] byteArray2 = bytes2.array();
            int byte1Max = bytes1.limit();
            int byte2Max = bytes2.limit();
            while (i < byte1Max && j < byte2Max) {
                if (byteArray1[i] < byteArray2[j]) {
                    results.put(byteArray1[i++]);
                }
                else {
                    results.put(byteArray2[j++]);
                }
            }
            if (i < byte1Max) {
                results.put(byteArray1, i, byte1Max - i);
            }
            else if (j < byte2Max) {
                results.put(byteArray2, j, byte2Max - j);
            }
            results.flip();
            return new ByteBufferStreamable(results);
        }
View Full Code Here

        final int nShortByteSize = Short.SIZE / Byte.SIZE;
        try {
            load_from_file(szUnicharFile);
            loadPffmTable(szPfmTable);
            FileInputStream fis = new FileInputStream(szIntTempFile);
            ByteBuffer bb = ByteBuffer.allocate(nIntByteSize * 3);
            ByteOrder bo = ByteOrder.BIG_ENDIAN;
            fis.read(bb.array());
            IntBuffer ib = bb.asIntBuffer();
            boolean swap = (ib.get(2) < 0 || ib.get(2) > MAX_NUM_CLASS_PRUNERS);
            if (swap) {
                bo = ByteOrder.LITTLE_ENDIAN;
                bb.order(bo);
                ib = bb.asIntBuffer();
            }
            int unicharset_size = ib.get(0);
            int NumClasses = ib.get(1);
            int NumClassPruners = ib.get(2);
            int version_id = 0;
            if (NumClasses < 0) {
                // handle version id
                version_id = -NumClasses;
                fis.read(bb.array(), 0, 4);
                NumClasses = ib.get(0);
            }   
            //this.ClassPruner.ensureCapacity(NumClassPruners);
            bb = ByteBuffer.allocate(nShortByteSize * unicharset_size);
            bb.order(bo);
            fis.read(bb.array());
            ShortBuffer sb = bb.asShortBuffer();
            for (int i=0; i<unicharset_size; i++) {
                this.IndexFor[i] = sb.get(i);
            }
            bb = ByteBuffer.allocate(nIntByteSize * NumClasses);
            bb.order(bo);
            fis.read(bb.array());
            ib = bb.asIntBuffer();
            for (int i=0; i<NumClasses; i++) {
                this.ClassIdFor[i] = ib.get(i);
            }
            ArrayList<byte[][][][]> ClassPruners = new ArrayList<byte[][][][]>();
            for (int i=0; i<NumClassPruners; i++) {
                byte[][][][] Pruner =
                        new byte[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][];
                bb = ByteBuffer.allocate(nIntByteSize * NUM_CP_BUCKETS *
                        NUM_CP_BUCKETS * NUM_CP_BUCKETS * WERDS_PER_CP_VECTOR);
                bb.order(bo);
                fis.read(bb.array());
                ib = bb.asIntBuffer();
                int p = 0;
                for (int j=0; j<NUM_CP_BUCKETS; j++) {
                    for (int k=0; k<NUM_CP_BUCKETS; k++) {
                        for (int l=0; l<NUM_CP_BUCKETS; l++) {
                            Pruner[j][k][l] =
View Full Code Here

     * @param bo the bo
     */
    LearntFeature(FileInputStream fis, ByteOrder bo) {
        try {
            final int nIntByteSize = Integer.SIZE / Byte.SIZE;
            ByteBuffer bb = ByteBuffer.allocate(4 +
                    nIntByteSize * WERDS_PER_CONFIG_VEC);
            bb.order(bo);
            fis.read(bb.array());
            this.A = bb.get(0) << 1;
            // Java doesn't have unsigned byte
            this.B = unsignedByteToShort(bb.get(1));
            this.C = bb.get(2) << 9;
            this.Angle = unsignedByteToShort(bb.get(3));
            int[] nInts = new int[WERDS_PER_CONFIG_VEC];
            IntBuffer ib = bb.asIntBuffer();
            for (int i=0; i<WERDS_PER_CONFIG_VEC; i++) {
                nInts[i] = ib.get(i+1);
            }
            setConfigBits(nInts);
        } catch (IOException ex) {
View Full Code Here

            int length=Integer.parseInt(headers.get("content-length"));
            body=new byte[length];
            in.read(body, 0, body.length);
        }
        else {
            ByteBuffer buf=ByteBuffer.allocate(500);
            boolean terminate=false;
            for(;;) {
                int c=in.read();
                if(c == -1 || c == 0)
                    terminate=true;

                if(buf.remaining() == 0 || terminate) {
                    if(body == null) {
                        body=new byte[buf.position()];
                        System.arraycopy(buf.array(), buf.arrayOffset(), body, 0, buf.position());
                    }
                    else {
                        byte[] tmp=new byte[body.length + buf.position()];
                        System.arraycopy(body, 0, tmp, 0, body.length);
                        try {
                            System.arraycopy(buf.array(), buf.arrayOffset(), tmp, body.length, buf.position());
                        }
                        catch(Throwable t) {
                        }
                        body=tmp;
                    }
                    buf.rewind();
                }

                if(terminate)
                    break;

                buf.put((byte)c);
            }
        }
        return new Frame(verb, headers, body);
    }
View Full Code Here

                len+=entry.getValue().length() +2;
                len+=5; // fill chars, such as ": " or "\n"
            }
        }
        len+=(buffer != null? 20 : 0);
        ByteBuffer buf=ByteBuffer.allocate(len);

        StringBuilder sb=new StringBuilder(ServerVerb.MESSAGE.name()).append("\n");
        if(headers != null) {
            for(Map.Entry<String,String> entry: headers.entrySet())
                sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
        }

        if(buffer != null)
            sb.append("content-length: ").append(String.valueOf(length)).append("\n");
        sb.append("\n");

        byte[] tmp=sb.toString().getBytes();

        if(buffer != null) {
            buf.put(tmp, 0, tmp.length);
            buf.put(buffer, offset, length);
        }
        buf.put(NULL_BYTE);

        final Set<Connection> target_connections=new HashSet<Connection>();
        String destination=headers != null? headers.get("destination") : null;
        if(destination == null) {
            synchronized(connections) {
                target_connections.addAll(connections);
            }
        }
        else {
            if(!exact_destination_match) {
                for(Map.Entry<String,Set<Connection>> entry: subscriptions.entrySet()) {
                    if(entry.getKey().startsWith(destination))
                        target_connections.addAll(entry.getValue());
                }
            }
            else {
                Set<Connection> conns=subscriptions.get(destination);
                if(conns != null)
                    target_connections.addAll(conns);
            }
        }

        for(Connection conn: target_connections)
            conn.writeResponse(buf.array(), buf.arrayOffset(), buf.position());
    }
View Full Code Here

    this.mCh = chUnichar;
    this.mnCutoff = nCutoff;
    try {
      final int nIntByteSize = Integer.SIZE / Byte.SIZE;
      final int nShortByteSize = Short.SIZE / Byte.SIZE;
      ByteBuffer bb = ByteBuffer.allocate(nShortByteSize + 2);
      bb.order(bo);
      fis.read(bb.array());
      this.NumProtos = bb.getShort(0);
      short NumProtoSets = LearntFeature.unsignedByteToShort(bb
          .get(nShortByteSize));
      this.NumConfigs = LearntFeature.unsignedByteToShort(bb
          .get(nShortByteSize + 1));
      if (nVersionId == 0) {
        // Only version 0 writes 5 pointless pointers to the file.
        // skip 5 ints
        bb = ByteBuffer.allocate(5 * nIntByteSize);
        fis.read(bb.array());
      }
      bb = ByteBuffer.allocate(MAX_NUM_CONFIGS * nShortByteSize);
      bb.order(bo);
      fis.read(bb.array());
      ShortBuffer sb = bb.asShortBuffer();
      for (int i = 0; i < this.ConfigLengths.length; i++) {
        this.ConfigLengths[i] = sb.get(i);
      }
      this.ProtoLengths = new short[NumProtoSets
          * Prototype.PROTOS_PER_PROTO_SET];
      Arrays.fill(this.ProtoLengths, (short) 0);
      bb = ByteBuffer.allocate(NumProtoSets
          * Prototype.PROTOS_PER_PROTO_SET);
      bb.order(bo);
      fis.read(bb.array());
      for (int i = 0; i < this.NumProtos; i++) {
        this.ProtoLengths[i] = LearntFeature.unsignedByteToShort(bb.get(i));
      }
      int nProtosLeft = this.NumProtos;
      for (int i = 0; i < NumProtoSets; i++) {
        this.add(new Prototype(fis, bo, Math.min(
            Prototype.PROTOS_PER_PROTO_SET, nProtosLeft)));
View Full Code Here

     * @param nProtosToRead the n protos to read
     */
    public Prototype(FileInputStream fis, ByteOrder bo, int nProtosToRead) {
        try {
            final int nIntByteSize = Integer.SIZE / Byte.SIZE;
            ByteBuffer bb = ByteBuffer.allocate(nIntByteSize * NUM_PP_PARAMS *
                    NUM_PP_BUCKETS * WERDS_PER_PP_VECTOR);
            bb.order(bo);
            fis.read(bb.array());
            IntBuffer ib = bb.asIntBuffer();
            int p = 0;
            for (int i=0; i<NUM_PP_PARAMS; i++) {
                for (int j=0; j<NUM_PP_BUCKETS; j++) {
                    for (int k=0; k<WERDS_PER_PP_VECTOR; k++) {
                        int nWord = ib.get(p++);
View Full Code Here

        int buflen = bFilePath.length + 29;//+ 4 + 4 + 4 + 8 + 8 + 1;
        if(buflen > RemotePagingService.MAX_COMMAND_BUFLEN) {
            throw new IllegalStateException("command size exceeds limit in MAX_COMMAND_BUFLEN("
                    + RemotePagingService.MAX_COMMAND_BUFLEN + "): " + buflen + " bytes");
        }
        ByteBuffer oprBuf = ByteBuffer.allocate(buflen);
        oprBuf.putInt(buflen - 4);
        oprBuf.putInt(RemotePagingService.COMMAND_READ);
        oprBuf.putInt(bFilePath.length); // #1
        oprBuf.put(bFilePath); // #2
        oprBuf.putLong(startOffset); // #3
        oprBuf.putLong(endOffset); // #4
        oprBuf.put((byte) (_bigEndian ? 1 : 0)); // # 5 is big endian?
        oprBuf.flip();
        NIOUtils.writeFully(channel, oprBuf);
    }
View Full Code Here

     */
    public void returnBuffer(Buffer buffer)
    {
        buffer.clear();
        int size=buffer.capacity();
        ByteBuffer bbuf = ((NIOBuffer)buffer).getByteBuffer();
        bbuf.position(0);
        bbuf.limit(size);
       
        if (size==_applicationBufferSize)
            _applicationBuffers.add(buffer);
        else if (size==_packetBufferSize)
            _packetBuffers.add(buffer);
View Full Code Here

TOP

Related Classes of java.nio.ByteBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.