List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:com.healthmarketscience.jackcess.impl.PageChannel.java
/** * @param buffer Buffer to read the page into * @param pageNumber Number of the page to read in (starting at 0) *///w w w.j a v a 2 s.c o m public void readPage(ByteBuffer buffer, int pageNumber) throws IOException { validatePageNumber(pageNumber); ByteBuffer inPage = buffer; ByteBuffer outPage = buffer; if ((pageNumber != 0) && !_codecHandler.canDecodeInline()) { inPage = _tempDecodeBufferH.getPageBuffer(this); outPage.clear(); } inPage.clear(); int bytesRead = _channel.read(inPage, (long) pageNumber * (long) getFormat().PAGE_SIZE); inPage.flip(); if (bytesRead != getFormat().PAGE_SIZE) { throw new IOException("Failed attempting to read " + getFormat().PAGE_SIZE + " bytes from page " + pageNumber + ", only read " + bytesRead); } if (pageNumber == 0) { // de-mask header (note, page 0 never has additional encoding) applyHeaderMask(buffer); } else { _codecHandler.decodePage(inPage, outPage, pageNumber); } }
From source file:com.github.ambry.utils.UtilsTest.java
@Test public void testReadStrings() throws IOException { // good case//from w w w. jav a 2 s. c o m ByteBuffer buffer = ByteBuffer.allocate(10); buffer.putShort((short) 8); String s = getRandomString(8); buffer.put(s.getBytes()); buffer.flip(); String outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer))); Assert.assertEquals(s, outputString); // 0-length buffer.rewind(); buffer.putShort(0, (short) 0); outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer))); Assert.assertTrue(outputString.isEmpty()); // failed case buffer.rewind(); buffer.putShort((short) 10); buffer.put(s.getBytes()); buffer.flip(); try { outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer))); Assert.assertTrue(false); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } buffer.rewind(); buffer.putShort(0, (short) -1); try { outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer))); Assert.fail("Should have encountered exception with negative length."); } catch (IllegalArgumentException e) { } // good case buffer = ByteBuffer.allocate(40004); buffer.putInt(40000); s = getRandomString(40000); buffer.put(s.getBytes()); buffer.flip(); outputString = Utils.readIntString(new DataInputStream(new ByteBufferInputStream(buffer)), StandardCharsets.UTF_8); Assert.assertEquals(s, outputString); // 0-length buffer.rewind(); buffer.putInt(0, 0); outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer))); Assert.assertTrue(outputString.isEmpty()); // failed case buffer.rewind(); buffer.putInt(50000); buffer.put(s.getBytes()); buffer.flip(); try { outputString = Utils.readIntString(new DataInputStream(new ByteBufferInputStream(buffer)), StandardCharsets.UTF_8); fail("Should have failed"); } catch (IllegalArgumentException e) { // expected. } buffer.rewind(); buffer.putInt(0, -1); try { Utils.readIntString(new DataInputStream(new ByteBufferInputStream(buffer)), StandardCharsets.UTF_8); Assert.fail("Should have encountered exception with negative length."); } catch (IllegalArgumentException e) { // expected. } }
From source file:org.sglover.checksum.ChecksumServiceImpl.java
@Override public NodeChecksums getChecksums(final Node node, final InputStream in) { final String nodeId = node.getNodeId(); final Long nodeVersion = node.getNodeVersion(); final Long nodeInternalId = node.getNodeInternalId(); final String versionLabel = node.getVersionLabel(); int x = 0;// www . ja v a 2 s . com NodeChecksums documentChecksums = new NodeChecksums(nodeId, nodeInternalId, nodeVersion, versionLabel, blockSize); try (ReadableByteChannel fc = getChannel(in)) { ByteBuffer data = ByteBuffer.allocate(blockSize * 20); int bytesRead = -1; int blockNum = 1; // starts at 1 do { bytesRead = fc.read(data); if (bytesRead > 0) { x += bytesRead; data.flip(); long numBlocks = data.limit() / blockSize + (data.limit() % blockSize > 0 ? 1 : 0); // spin through the data and create checksums for each block for (int i = 0; i < numBlocks; i++) { int start = i * blockSize; int end = start + blockSize - 1; if (end >= data.limit()) { end = data.limit() - 1; } // calculate the adler32 checksum Adler32 adlerInfo = new Adler32(data, start, end, hasher); // calculate the full md5 checksum String md5sum = hasher.md5(data, start, end); Checksum checksum = new Checksum(blockNum, start, end, adlerInfo.getHash(), adlerInfo.getAdler32(), md5sum); if (blockNum < 2) { System.out.println(checksum); } documentChecksums.addChecksum(checksum); blockNum++; } data.clear(); } } while (bytesRead > 0); } catch (NoSuchAlgorithmException | IOException e) { throw new RuntimeException(e); } return documentChecksums; }
From source file:net.timewalker.ffmq4.transport.tcp.nio.NIOTcpMultiplexer.java
protected boolean readAndProcessChannelData(NIOClientSocketHandler clientHandler) { try {// w w w . j ava 2s . c o m ByteBuffer inputBuffer = clientHandler.getInputBuffer(); int readAmount = clientHandler.getSocketChannel().read(inputBuffer); if (readAmount <= 0) { log.debug("[" + clientHandler.getId() + "] Cannot read, channel socket was closed"); return false; } inputBuffer.flip(); // Prepare for reading boolean status = clientHandler.handleIncomingData(); inputBuffer.compact(); // Restore pointers return status; } catch (IOException e) { log.debug("[" + clientHandler.getId() + "] Read failed : " + e.getMessage()); return false; } catch (Exception e) { log.error("[" + clientHandler.getId() + "] Could not read channel data", e); return false; } }
From source file:org.apache.hadoop.hbase.client.TestResult.java
/** * Microbenchmark that compares {@link Result#getValue} and {@link Result#loadValue} performance. * * @throws Exception//from w w w . j a va2 s. c o m */ public void doReadBenchmark() throws Exception { final int n = 5; final int m = 100000000; StringBuilder valueSB = new StringBuilder(); for (int i = 0; i < 100; i++) { valueSB.append((byte) (Math.random() * 10)); } StringBuilder rowSB = new StringBuilder(); for (int i = 0; i < 50; i++) { rowSB.append((byte) (Math.random() * 10)); } KeyValue[] kvs = genKVs(Bytes.toBytes(rowSB.toString()), family, Bytes.toBytes(valueSB.toString()), 1, n); Arrays.sort(kvs, KeyValue.COMPARATOR); ByteBuffer loadValueBuffer = ByteBuffer.allocate(1024); Result r = Result.create(kvs); byte[][] qfs = new byte[n][Bytes.SIZEOF_INT]; for (int i = 0; i < n; ++i) { System.arraycopy(qfs[i], 0, Bytes.toBytes(i), 0, Bytes.SIZEOF_INT); } // warm up for (int k = 0; k < 100000; k++) { for (int i = 0; i < n; ++i) { r.getValue(family, qfs[i]); loadValueBuffer.clear(); r.loadValue(family, qfs[i], loadValueBuffer); loadValueBuffer.flip(); } } System.gc(); long start = System.nanoTime(); for (int k = 0; k < m; k++) { for (int i = 0; i < n; ++i) { loadValueBuffer.clear(); r.loadValue(family, qfs[i], loadValueBuffer); loadValueBuffer.flip(); } } long stop = System.nanoTime(); System.out.println("loadValue(): " + (stop - start)); System.gc(); start = System.nanoTime(); for (int k = 0; k < m; k++) { for (int i = 0; i < n; i++) { r.getValue(family, qfs[i]); } } stop = System.nanoTime(); System.out.println("getValue(): " + (stop - start)); }
From source file:co.paralleluniverse.galaxy.core.BackupImpl.java
private Message.BACKUP makeBackup(CacheLine line, long version) { if (line.getVersion() != version) return null; final Message.BACKUP backup; if (line.getData() == null) { backup = Message.BACKUP(line.getId(), line.getVersion(), null); } else {// ww w.j av a2 s. co m final ByteBuffer buffer = ByteBuffer.allocate(line.getData().limit()); // storage.allocateStorage(line.getData().limit()); line.rewind(); buffer.put(line.getData()); line.rewind(); buffer.flip(); backup = Message.BACKUP(line.getId(), line.getVersion(), buffer); } LOG.debug("Copying version {} of line {} data: {}", new Object[] { backup.getVersion(), hex(backup.getLine()), backup.getData() != null ? "(" + backup.getData().remaining() + " bytes)" : "null" }); return backup; }
From source file:com.clustercontrol.agent.job.PublicKeyThread.java
/** * ?Authorized_key????<BR>//w w w . j a v a 2 s .c o m * * @param publicKey * @return true : ?false: */ private synchronized boolean deleteKey(String publicKey) { m_log.debug("delete key start"); if (SKIP_KEYFILE_UPDATE) { m_log.info("skipped deleting publicKey"); return true; } Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); CharsetDecoder decoder = charset.newDecoder(); //??? String fileName = AgentProperties.getProperty(execUser.toLowerCase() + AUTHORIZED_KEY_PATH); if (fileName == null || fileName.length() == 0) return false; //File? File fi = new File(fileName); RandomAccessFile randomAccessFile = null; FileChannel channel = null; FileLock lock = null; boolean delete = false; try { //RandomAccessFile? randomAccessFile = new RandomAccessFile(fi, "rw"); //FileChannel? channel = randomAccessFile.getChannel(); // for (int i = 0; i < (FILELOCK_TIMEOUT / FILELOCK_WAIT); i++) { if (null != (lock = channel.tryLock())) { break; } m_log.info("waiting for locked file... [" + (i + 1) + "/" + (FILELOCK_TIMEOUT / FILELOCK_WAIT) + " : " + fileName + "]"); Thread.sleep(FILELOCK_WAIT); } if (null == lock) { m_log.warn("file locking timeout."); return false; } // (?) synchronized (authKeyLock) { //?? ByteBuffer buffer = ByteBuffer.allocate((int) channel.size()); //?? channel.read(buffer); // ???????????0? buffer.flip(); //?? String contents = decoder.decode(buffer).toString(); // ? m_log.debug("contents " + contents.length() + " : " + contents); //?? List<String> keyCheck = new ArrayList<String>(); StringTokenizer tokenizer = new StringTokenizer(contents, "\n"); while (tokenizer.hasMoreTokens()) { keyCheck.add(tokenizer.nextToken()); } //?????? int s = keyCheck.lastIndexOf(publicKey); if (s != -1) { // ? m_log.debug("remobe key : " + keyCheck.get(s)); keyCheck.remove(s); } //????? encoder.reset(); buffer.clear(); int i; if (keyCheck.size() > 0) { for (i = 0; i < keyCheck.size() - 1; i++) { encoder.encode(CharBuffer.wrap(keyCheck.get(i) + "\n"), buffer, false); } encoder.encode(CharBuffer.wrap(keyCheck.get(i)), buffer, true); } //??? buffer.flip(); channel.truncate(0); channel.position(0); channel.write(buffer); } delete = true; } catch (IOException e) { m_log.error(e.getMessage(), e); } catch (RuntimeException e) { m_log.error(e.getMessage(), e); } catch (InterruptedException e) { m_log.error(e.getMessage(), e); } finally { try { if (channel != null) { channel.close(); } if (randomAccessFile != null) { randomAccessFile.close(); } //? if (lock != null) { lock.release(); } } catch (Exception e) { } } return delete; }
From source file:com.google.cloud.hadoop.gcsio.GoogleCloudStorageIntegrationHelper.java
/** * Helper which reads the entire file as a String. *//*from w ww. j ava 2s. c o m*/ protected String readTextFile(String bucketName, String objectName) throws IOException { SeekableReadableByteChannel readChannel = null; ByteBuffer readBuffer = ByteBuffer.allocate(1024); StringBuffer returnBuffer = new StringBuffer(); try { readChannel = open(bucketName, objectName); int numBytesRead = readChannel.read(readBuffer); while (numBytesRead > 0) { readBuffer.flip(); returnBuffer.append(StandardCharsets.UTF_8.decode(readBuffer)); readBuffer.clear(); numBytesRead = readChannel.read(readBuffer); } } finally { if (readChannel != null) { readChannel.close(); } } return returnBuffer.toString(); }
From source file:org.apache.bookkeeper.bookie.BookieJournalTest.java
private void writePreV2Journal(File journalDir, int numEntries) throws Exception { long logId = System.currentTimeMillis(); File fn = new File(journalDir, Long.toHexString(logId) + ".txn"); FileChannel fc = new RandomAccessFile(fn, "rw").getChannel(); ByteBuffer zeros = ByteBuffer.allocate(512); fc.write(zeros, 4 * 1024 * 1024);//from w w w . ja v a2 s .c o m fc.position(0); byte[] data = "JournalTestData".getBytes(); long lastConfirmed = LedgerHandle.INVALID_ENTRY_ID; for (int i = 1; i <= numEntries; i++) { ByteBuffer packet = ClientUtil.generatePacket(1, i, lastConfirmed, i * data.length, data) .toByteBuffer(); lastConfirmed = i; ByteBuffer lenBuff = ByteBuffer.allocate(4); lenBuff.putInt(packet.remaining()); lenBuff.flip(); fc.write(lenBuff); fc.write(packet); } }
From source file:com.serenegiant.media.TLMediaEncoder.java
/** * read raw bit stream from specific intermediate file * @param in//from w w w.ja v a2s .c om * @param header * @param buffer * @param readBuffer * @throws IOException * @throws BufferOverflowException */ /*package*/static ByteBuffer readStream(final DataInputStream in, final TLMediaFrameHeader header, ByteBuffer buffer, final byte[] readBuffer) throws IOException { readHeader(in, header); if ((buffer == null) || header.size > buffer.capacity()) { buffer = ByteBuffer.allocateDirect(header.size); } buffer.clear(); final int max_bytes = Math.min(readBuffer.length, header.size); int read_bytes; for (int i = header.size; i > 0; i -= read_bytes) { read_bytes = in.read(readBuffer, 0, Math.min(i, max_bytes)); if (read_bytes <= 0) break; buffer.put(readBuffer, 0, read_bytes); } buffer.flip(); return buffer; }