Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

In this page you can find the example usage for java.nio ByteBuffer get.

Prototype

public ByteBuffer get(byte[] dest, int off, int len) 

Source Link

Document

Reads bytes from the current position into the specified byte array, starting at the specified offset, and increases the position by the number of bytes read.

Usage

From source file:au.org.ala.delta.io.BinFile.java

public int readBytes(byte[] buffer) {
    if (_buffer != null) {
        for (int i = 0; i < buffer.length; ++i) {
            buffer[i] = _buffer[_filePointer];
            _filePointer++;//from  w  w w  .j  a  va 2s .  co m
        }
        return buffer.length;
    } else {
        try {
            ByteBuffer bb = ByteBuffer.allocate(buffer.length);
            int bytesRead = _channel.read(bb);
            bb.position(0);
            bb.get(buffer, 0, buffer.length);
            return bytesRead;
        } catch (IOException ioex) {
            throw new RuntimeException(ioex);
        }
    }
}

From source file:com.rs.worldserver.io.IOClient.java

public void read(ByteBuffer buffer) {
    inStream.currentOffset = 0;/*ww  w  .j  av  a2 s.  c o m*/
    buffer.flip();
    buffer.get(inStream.buffer, 0, buffer.limit());
    inStream.length = buffer.limit();
    try {
        try {
            System.out.println("Processing");
            process();
        } catch (Exception e) {
            e.printStackTrace();
            Server.getIoThread().destroySocket(Server.getIoThread().socketFor(this), connectedFrom.toString(),
                    true);
        }
    } catch (Exception e) {
    }
}

From source file:net.cellcloud.talk.stuff.PrimitiveSerializer.java

/** ??
 *//*from   www.j a  va  2s . co  m*/
public static void write(OutputStream stream, Primitive primitive) {
    /*
    ??
    [version]{sutff}...{stuff}[dialect@tracker]
    
    [01.00]{sub=cloud:string}{pre=add:string}[Action@Ambrose]
    */

    try {
        // 
        stream.write((int) TOKEN_OPEN_BRACKET);
        byte[] version = { '0', '1', TOKEN_POINT, '0', '0' };
        stream.write(version);
        stream.write((int) TOKEN_CLOSE_BRACKET);

        ByteBuffer buf = ByteBuffer.allocate(BLOCK);
        int bufLength = 0;

        // 
        List<SubjectStuff> subjects = primitive.subjects();
        if (null != subjects) {
            Iterator<SubjectStuff> iter = subjects.iterator();
            while (iter.hasNext()) {
                SubjectStuff stuff = iter.next();
                stream.write((int) TOKEN_OPEN_BRACE);
                stream.write(STUFFTYPE_SUBJECT_BYTES);
                stream.write((int) TOKEN_OPERATE_ASSIGN);

                bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8")));
                buf.flip();
                byte[] d = new byte[bufLength];
                buf.get(d, 0, bufLength);
                stream.write(d);
                buf.clear();

                stream.write((int) TOKEN_OPERATE_DECLARE);
                stream.write(parseLiteralBase(stuff.literalBase));
                stream.write((int) TOKEN_CLOSE_BRACE);
            }
        }
        List<PredicateStuff> predicates = primitive.predicates();
        if (null != predicates) {
            Iterator<PredicateStuff> iter = predicates.iterator();
            while (iter.hasNext()) {
                PredicateStuff stuff = iter.next();
                stream.write((int) TOKEN_OPEN_BRACE);
                stream.write(STUFFTYPE_PREDICATE_BYTES);
                stream.write((int) TOKEN_OPERATE_ASSIGN);

                bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8")));
                buf.flip();
                byte[] d = new byte[bufLength];
                buf.get(d, 0, bufLength);
                stream.write(d);
                buf.clear();

                stream.write((int) TOKEN_OPERATE_DECLARE);
                stream.write(parseLiteralBase(stuff.literalBase));
                stream.write((int) TOKEN_CLOSE_BRACE);
            }
        }
        List<ObjectiveStuff> objectives = primitive.objectives();
        if (null != objectives) {
            Iterator<ObjectiveStuff> iter = objectives.iterator();
            while (iter.hasNext()) {
                ObjectiveStuff stuff = iter.next();
                stream.write((int) TOKEN_OPEN_BRACE);
                stream.write(STUFFTYPE_OBJECTIVE_BYTES);
                stream.write((int) TOKEN_OPERATE_ASSIGN);

                bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8")));
                buf.flip();
                byte[] d = new byte[bufLength];
                buf.get(d, 0, bufLength);
                stream.write(d);
                buf.clear();

                stream.write((int) TOKEN_OPERATE_DECLARE);
                stream.write(parseLiteralBase(stuff.literalBase));
                stream.write((int) TOKEN_CLOSE_BRACE);
            }
        }
        List<AdverbialStuff> adverbials = primitive.adverbials();
        if (null != adverbials) {
            Iterator<AdverbialStuff> iter = adverbials.iterator();
            while (iter.hasNext()) {
                AdverbialStuff stuff = iter.next();
                stream.write((int) TOKEN_OPEN_BRACE);
                stream.write(STUFFTYPE_ADVERBIAL_BYTES);
                stream.write((int) TOKEN_OPERATE_ASSIGN);

                bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8")));
                buf.flip();
                byte[] d = new byte[bufLength];
                buf.get(d, 0, bufLength);
                stream.write(d);
                buf.clear();

                stream.write((int) TOKEN_OPERATE_DECLARE);
                stream.write(parseLiteralBase(stuff.literalBase));
                stream.write((int) TOKEN_CLOSE_BRACE);
            }
        }
        List<AttributiveStuff> attributives = primitive.attributives();
        if (null != attributives) {
            Iterator<AttributiveStuff> iter = attributives.iterator();
            while (iter.hasNext()) {
                AttributiveStuff stuff = iter.next();
                stream.write((int) TOKEN_OPEN_BRACE);
                stream.write(STUFFTYPE_ATTRIBUTIVE_BYTES);
                stream.write((int) TOKEN_OPERATE_ASSIGN);

                bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8")));
                buf.flip();
                byte[] d = new byte[bufLength];
                buf.get(d, 0, bufLength);
                stream.write(d);
                buf.clear();

                stream.write((int) TOKEN_OPERATE_DECLARE);
                stream.write(parseLiteralBase(stuff.literalBase));
                stream.write((int) TOKEN_CLOSE_BRACE);
            }
        }
        List<ComplementStuff> complements = primitive.complements();
        if (null != complements) {
            Iterator<ComplementStuff> iter = complements.iterator();
            while (iter.hasNext()) {
                ComplementStuff stuff = iter.next();
                stream.write((int) TOKEN_OPEN_BRACE);
                stream.write(STUFFTYPE_COMPLEMENT_BYTES);
                stream.write((int) TOKEN_OPERATE_ASSIGN);

                bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8")));
                buf.flip();
                byte[] d = new byte[bufLength];
                buf.get(d, 0, bufLength);
                stream.write(d);
                buf.clear();

                stream.write((int) TOKEN_OPERATE_DECLARE);
                stream.write(parseLiteralBase(stuff.literalBase));
                stream.write((int) TOKEN_CLOSE_BRACE);
            }
        }

        // 
        Dialect dialect = primitive.getDialect();
        if (null != dialect) {
            stream.write(TOKEN_OPEN_BRACKET);
            stream.write(dialect.getName().getBytes(Charset.forName("UTF-8")));
            stream.write(TOKEN_AT);
            stream.write(dialect.getTracker().getBytes(Charset.forName("UTF-8")));
            stream.write(TOKEN_CLOSE_BRACKET);
        }

        stream.flush();
    } catch (IOException e) {
        Logger.log(PrimitiveSerializer.class, e, LogLevel.ERROR);
    }
}

From source file:com.mellanox.jxio.EventQueueHandler.java

private String readString(ByteBuffer buf) {
    int len = buf.getInt();
    byte b[] = new byte[len];
    buf.get(b, 0, len);
    String s1 = new String(b, Charset.forName("US-ASCII"));
    return s1;//from w  ww. j ava 2 s  . c  o m
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

private void appendSitemapIndex(String sitemap) throws IOException {
    File sitemapIndexFile = new File(mapResourcesRoot.getFile(), SITEMAP_INDEX_NAME);
    if (!sitemapIndexFile.exists()) {
        //if a new sitemap file
        List<String> lines = new ArrayList<String>();
        lines.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        lines.add("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
        lines.add("</sitemapindex>");
        FileUtils.writeLines(sitemapIndexFile, lines);
    }//  ww w .  j  ava2 s  . c  o  m

    RandomAccessFile rfile = new RandomAccessFile(sitemapIndexFile, "rw");
    FileChannel channel = rfile.getChannel();

    //this new content will append to end of file before XML end tag
    StringBuilder lines = new StringBuilder();
    lines.append("   <sitemap>\n");
    lines.append("     <loc>" + WebUtil.getHostAppURL() + SITEMAP_URL_CONTEXT + sitemap + "</loc>\n");
    lines.append("     <lastmod>" + TIME_FORMAT.format(new Date()) + " </lastmod>\n");
    lines.append("   </sitemap>\n");
    //the last tag will be overwrite, so append it again to new content. 
    lines.append(SITEMAP_INDEX_TAIL_FLAG);
    byte[] content = lines.toString().getBytes();

    ByteBuffer byteBuf = ByteBuffer.allocate(512);
    // seek first
    int len = 0, headIdx = 0;
    long tailIdx = channel.size() - 512;
    tailIdx = tailIdx < 0 ? 0 : tailIdx;

    long headPos = -1;
    StringBuilder header = new StringBuilder();
    while ((len = channel.read(byteBuf, tailIdx)) > 0) {
        byteBuf.rewind();
        byte[] dst = new byte[len];
        byteBuf.get(dst, 0, len);
        header.append(new String(dst, "UTF8"));
        headIdx = header.indexOf(SITEMAP_INDEX_TAIL_FLAG);
        if (headIdx != -1) {
            headPos = channel.size() - header.substring(headIdx).getBytes().length;
            break;
        }
    }
    FileLock lock = channel.tryLock(headPos, content.length, false);
    try {
        channel.write(ByteBuffer.wrap(content), headPos);
    } finally {
        lock.release();
    }

    channel.force(false);
    rfile.close();

}

From source file:com.mellanox.hadoop.mapred.UdaPlugin.java

public void dataFromUda(Object directBufAsObj, int len) throws Throwable {
    if (LOG.isDebugEnabled())
        LOG.debug("-->> dataFromUda len=" + len);

    KVBuf buf = kv_bufs[__cur_kv_idx__];

    synchronized (buf) {
        while (buf.status != kv_buf_recv_ready) {
            try {
                buf.wait();/*from w  w  w  .  ja  v  a 2s.  c om*/
            } catch (InterruptedException e) {
            }
        }

        buf.act_len = len; // set merged size
        try {
            java.nio.ByteBuffer directBuf = (java.nio.ByteBuffer) directBufAsObj;
            directBuf.position(0); // reset read position         
            directBuf.get(buf.kv_buf, 0, len);// memcpy from direct buf into java buf - TODO: try zero-copy
        } catch (Throwable t) {
            LOG.error("!!! !! dataFromUda GOT Exception");
            LOG.error(StringUtils.stringifyException(t));
            throw (t);
        }

        buf.kv.reset(buf.kv_buf, 0, len); // reset KV read position

        buf.status = kv_buf_redc_ready;
        ++__cur_kv_idx__;
        if (__cur_kv_idx__ >= kv_buf_num) {
            __cur_kv_idx__ = 0;
        }
        buf.notifyAll();
    }
    if (LOG.isDebugEnabled())
        LOG.debug("<<-- dataFromUda finished callback");
}

From source file:com.zotoh.maedr.device.apache.StreamingNHttpEntity.java

private void sockItDown(ContentDecoder decoder) throws IOException {

    tlog().debug("StreamingNHttpEntity: sockItDown()");

    ByteBuffer buffer;
    int cnt;// w w w  . j  a  va  2  s.c o  m

    buffer = _alloctor.allocate(4096);
    do {

        buffer.clear();

        if ((cnt = decoder.read(buffer)) == -1)
            break;

        if (cnt == 0) {

            if (buffer.hasRemaining())
                break;
            else
                continue;
        }

        // 
        buffer.flip();
        byte[] bits = new byte[4096];
        int len;

        while (buffer.hasRemaining()) {
            len = Math.min(4096, buffer.remaining());
            buffer.get(bits, 0, len);
            storeBytes(bits, len);
        }
    } while (true);

}

From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapter.java

@Override
public void receive(ByteBuffer buf, String channelId) {
    buf.mark();//  www  . j a v  a2  s  . c  o m
    int size = buf.remaining();
    if (size < 1)
        return;
    byte[] data = new byte[size];
    buf.get(data, 0, size);
    //System.out.println(" \n");
    //System.out.println("Read " + size + " bytes");

    String xml = new String(data);
    parseUsingDocument(xml, channelId);
    //parseUsingStream(buf);
}

From source file:net.librec.data.convertor.TextDataConvertor.java

/**
 * Read data from the data file. Note that we didn't take care of the
 * duplicated lines./*from  w  w w.  j  a  va2s.  c  o m*/
 *
 * @param dataColumnFormat
 *            the format of input data file
 * @param inputDataPath
 *            the path of input data file
 * @param binThold
 *            the threshold to binarize a rating. If a rating is greater
 *            than the threshold, the value will be 1; otherwise 0. To
 *            disable this appender, i.e., keep the original rating value,
 *            set the threshold a negative value
 * @throws IOException
 *            if the <code>inputDataPath</code> is not valid.
 */
private void readData(String dataColumnFormat, String inputDataPath, double binThold) throws IOException {
    LOG.info(String.format("Dataset: %s", StringUtil.last(inputDataPath, 38)));
    // Table {row-id, col-id, rate}
    Table<Integer, Integer, Double> dataTable = HashBasedTable.create();
    // Table {row-id, col-id, timestamp}
    Table<Integer, Integer, Long> timeTable = null;
    // Map {col-id, multiple row-id}: used to fast build a rating matrix
    Multimap<Integer, Integer> colMap = HashMultimap.create();
    // BiMap {raw id, inner id} userIds, itemIds
    if (this.userIds == null) {
        this.userIds = HashBiMap.create();
    }
    if (this.itemIds == null) {
        this.itemIds = HashBiMap.create();
    }
    final List<File> files = new ArrayList<File>();
    final ArrayList<Long> fileSizeList = new ArrayList<Long>();
    SimpleFileVisitor<Path> finder = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            fileSizeList.add(file.toFile().length());
            files.add(file.toFile());
            return super.visitFile(file, attrs);
        }
    };
    Files.walkFileTree(Paths.get(inputDataPath), finder);
    LOG.info("All dataset files " + files.toString());
    long allFileSize = 0;
    for (Long everyFileSize : fileSizeList) {
        allFileSize = allFileSize + everyFileSize.longValue();
    }
    LOG.info("All dataset files size " + Long.toString(allFileSize));
    int readingFileCount = 0;
    long loadAllFileByte = 0;
    // loop every dataFile collecting from walkFileTree
    for (File dataFile : files) {
        LOG.info("Now loading dataset file " + dataFile.toString().substring(
                dataFile.toString().lastIndexOf(File.separator) + 1, dataFile.toString().lastIndexOf(".")));
        readingFileCount += 1;
        loadFilePathRate = readingFileCount / (float) files.size();
        long readingOneFileByte = 0;
        FileInputStream fis = new FileInputStream(dataFile);
        FileChannel fileRead = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
        int len;
        String bufferLine = new String();
        byte[] bytes = new byte[BSIZE];
        while ((len = fileRead.read(buffer)) != -1) {
            readingOneFileByte += len;
            loadDataFileRate = readingOneFileByte / (float) fileRead.size();
            loadAllFileByte += len;
            loadAllFileRate = loadAllFileByte / (float) allFileSize;
            buffer.flip();
            buffer.get(bytes, 0, len);
            bufferLine = bufferLine.concat(new String(bytes, 0, len));
            bufferLine = bufferLine.replaceAll("\r", "\n");
            String[] bufferData = bufferLine.split("(\n)+");
            boolean isComplete = bufferLine.endsWith("\n");
            int loopLength = isComplete ? bufferData.length : bufferData.length - 1;
            for (int i = 0; i < loopLength; i++) {
                String line = new String(bufferData[i]);
                String[] data = line.trim().split("[ \t,]+");
                String user = data[0];
                String item = data[1];
                Double rate = ((dataColumnFormat.equals("UIR") || dataColumnFormat.equals("UIRT"))
                        && data.length >= 3) ? Double.valueOf(data[2]) : 1.0;

                // binarize the rating for item recommendation task
                if (binThold >= 0) {
                    rate = rate > binThold ? 1.0 : 0.0;
                }

                // inner id starting from 0
                int row = userIds.containsKey(user) ? userIds.get(user) : userIds.size();
                userIds.put(user, row);

                int col = itemIds.containsKey(item) ? itemIds.get(item) : itemIds.size();
                itemIds.put(item, col);

                dataTable.put(row, col, rate);
                colMap.put(col, row);
                // record rating's issuing time
                if (StringUtils.equals(dataColumnFormat, "UIRT") && data.length >= 4) {
                    if (timeTable == null) {
                        timeTable = HashBasedTable.create();
                    }
                    // convert to million-seconds
                    long mms = 0L;
                    try {
                        mms = Long.parseLong(data[3]); // cannot format
                        // 9.7323480e+008
                    } catch (NumberFormatException e) {
                        mms = (long) Double.parseDouble(data[3]);
                    }
                    long timestamp = timeUnit.toMillis(mms);
                    timeTable.put(row, col, timestamp);
                }
            }
            if (!isComplete) {
                bufferLine = bufferData[bufferData.length - 1];
            }
            buffer.clear();
        }
        fileRead.close();
        fis.close();
    }
    int numRows = numUsers(), numCols = numItems();
    // build rating matrix
    preferenceMatrix = new SparseMatrix(numRows, numCols, dataTable, colMap);
    if (timeTable != null)
        datetimeMatrix = new SparseMatrix(numRows, numCols, timeTable, colMap);
    // release memory of data table
    dataTable = null;
    timeTable = null;
}

From source file:com.team254.cheezdroid.SelfieModeFragment.java

/**
 * This a callback object for the {@link ImageReader}. "onImageAvailable" will be called when a
 * still image is ready to be saved./*from w w w .  j a v a  2  s.  c o  m*/
 */

private byte[] convertYUV420ToN21(Image imgYUV420) {
    byte[] rez = new byte[0];

    ByteBuffer buffer0 = imgYUV420.getPlanes()[0].getBuffer();
    ByteBuffer buffer2 = imgYUV420.getPlanes()[2].getBuffer();
    int buffer0_size = buffer0.remaining();
    int buffer2_size = buffer2.remaining();
    rez = new byte[buffer0_size + buffer2_size];

    buffer0.get(rez, 0, buffer0_size);
    buffer2.get(rez, buffer0_size, buffer2_size);

    return rez;
}