List of usage examples for java.nio ByteBuffer putInt
public abstract ByteBuffer putInt(int value);
From source file:de.rwhq.btree.LeafNode.java
public void setNextLeafId(final Integer id) { final ByteBuffer buffer = rawPage().bufferForWriting(Header.NEXT_LEAF_ID.getOffset()); buffer.putInt(id == null ? NO_NEXT_LEAF : id); rawPage().sync();//from w ww . ja v a 2s .c o m }
From source file:de.rwhq.btree.InnerNode.java
public void initRootState(final Integer pageId1, final byte[] serializedKey, final Integer pageId2) { ensureValid();//from ww w .j a va2 s .c om validateLengthOfSerializedKey(serializedKey); final ByteBuffer buf = rawPage().bufferForWriting(Header.size()); buf.putInt(pageId1); buf.put(serializedKey); buf.putInt(pageId2); setNumberOfKeys(1); rawPage.sync(); }
From source file:de.rwhq.btree.InnerNode.java
/** * @param numberOfKeys//from www .ja va 2 s. com * the numberOfKeys to set */ private void setNumberOfKeys(final int numberOfKeys) { this.numberOfKeys = numberOfKeys; final ByteBuffer buf = rawPage.bufferForWriting(Header.NUMBER_OF_KEYS.getOffset()); buf.putInt(numberOfKeys); }
From source file:de.rwhq.btree.InnerNode.java
/** * @param rawKeys//www .j a v a 2s. co m * @param pageIds * @param fromId * @return */ public int bulkInitialize(final ArrayList<byte[]> rawKeys, final ArrayList<Integer> pageIds, final int fromId) { if (pageIds.size() < (fromId + 2) || rawKeys.size() != (pageIds.size() - 1)) throw new IllegalArgumentException( "for bulkinsert, you must have at least 2 page ids and keys.size() == (pageIds.size() - 1)\n" + "pageIds.size()=" + pageIds.size() + ";fromId=" + fromId + ";rawKeys.size()=" + rawKeys.size()); final int fromId2 = fromId; initialize(); final ByteBuffer buf = rawPage().bufferForWriting(Header.size()); buf.putInt(pageIds.get(fromId2)); final int requiredSpace = Integer.SIZE / 8 + rawKeys.get(0).length; final int spaceForEntries = buf.remaining() / requiredSpace; final int totalEntriesToInsert = (pageIds.size() - fromId - 1); int entriesToInsert = spaceForEntries < totalEntriesToInsert ? spaceForEntries : totalEntriesToInsert; // make sure that not exactly one pageId remains, because that can't be inserted alone in the next // InnerNode. == 2 because final int remaining = pageIds.size() - fromId - (entriesToInsert + 1); if (remaining == 1) entriesToInsert--; for (int i = 0; i < entriesToInsert; i++) { // System.out.println("fetching rawKey " + (fromId + i) + " from array length " + rawKeys.size() + " with i=" + i); buf.put(rawKeys.get(fromId + i)); // fromId + 1 - 1 +i //LOG.debug("insert key: " + keySerializer.deserialize(rawKeys.get(fromId + i))); buf.putInt(pageIds.get(fromId + 1 + i)); } setNumberOfKeys(entriesToInsert); rawPage.sync(); return entriesToInsert + 1; // page ids }
From source file:org.apache.jxtadoop.hdfs.server.datanode.BlockSender.java
/** * Sends upto maxChunks chunks of data.//from w w w . j av a2 s. c o m * * When blockInPosition is >= 0, assumes 'out' is a * {@link SocketOutputStream} and tries * {@link SocketOutputStream#transferToFully(FileChannel, long, int)} to * send data (and updates blockInPosition). */ private int sendChunks(ByteBuffer pkt, int maxChunks, OutputStream out) throws IOException { // Sends multiple chunks in one packet with a single write(). int len = Math.min((int) (endOffset - offset), bytesPerChecksum * maxChunks); if (len == 0) { return 0; } int numChunks = (len + bytesPerChecksum - 1) / bytesPerChecksum; int packetLen = len + numChunks * checksumSize + 4; pkt.clear(); // write packet header pkt.putInt(packetLen); pkt.putLong(offset); pkt.putLong(seqno); pkt.put((byte) ((offset + len >= endOffset) ? 1 : 0)); //why no ByteBuf.putBoolean()? pkt.putInt(len); int checksumOff = pkt.position(); int checksumLen = numChunks * checksumSize; byte[] buf = pkt.array(); if (checksumSize > 0 && checksumIn != null) { try { checksumIn.readFully(buf, checksumOff, checksumLen); } catch (IOException e) { LOG.warn(" Could not read or failed to veirfy checksum for data" + " at offset " + offset + " for block " + block + " got : " + StringUtils.stringifyException(e)); IOUtils.closeStream(checksumIn); checksumIn = null; if (corruptChecksumOk) { if (checksumOff < checksumLen) { // Just fill the array with zeros. Arrays.fill(buf, checksumOff, checksumLen, (byte) 0); } } else { throw e; } } } int dataOff = checksumOff + checksumLen; if (blockInPosition < 0) { //normal transfer IOUtils.readFully(blockIn, buf, dataOff, len); if (verifyChecksum) { int dOff = dataOff; int cOff = checksumOff; int dLeft = len; for (int i = 0; i < numChunks; i++) { checksum.reset(); int dLen = Math.min(dLeft, bytesPerChecksum); checksum.update(buf, dOff, dLen); if (!checksum.compare(buf, cOff)) { throw new ChecksumException("Checksum failed at " + (offset + len - dLeft), len); } dLeft -= dLen; dOff += dLen; cOff += checksumSize; } } //writing is done below (mainly to handle IOException) } try { if (blockInPosition >= 0) { //use transferTo(). Checks on out and blockIn are already done. SocketOutputStream sockOut = (SocketOutputStream) out; //first write the packet sockOut.write(buf, 0, dataOff); // no need to flush. since we know out is not a buffered stream. sockOut.transferToFully(((FileInputStream) blockIn).getChannel(), blockInPosition, len); blockInPosition += len; } else { // normal transfer out.write(buf, 0, dataOff + len); } } catch (IOException e) { /* exception while writing to the client (well, with transferTo(), * it could also be while reading from the local file). */ throw ioeToSocketException(e); } if (throttler != null) { // rebalancing so throttle throttler.throttle(packetLen); } return len; }
From source file:org.apache.bookkeeper.replication.AuditorLedgerCheckerTest.java
private void addEntry(int numEntriesToWrite, LedgerHandle lh) throws InterruptedException, BKException { final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite); final AtomicInteger rc = new AtomicInteger(BKException.Code.OK); for (int i = 0; i < numEntriesToWrite; i++) { ByteBuffer entry = ByteBuffer.allocate(4); entry.putInt(rng.nextInt(Integer.MAX_VALUE)); entry.position(0);// w w w .java 2 s .com lh.asyncAddEntry(entry.array(), new AddCallback() { public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) { rc.compareAndSet(BKException.Code.OK, rc2); completeLatch.countDown(); } }, null); } completeLatch.await(); if (rc.get() != BKException.Code.OK) { throw BKException.create(rc.get()); } }
From source file:com.warfrog.bitmapallthethings.BattEngine.java
private InputStream generateBitmapHeader(int width, int height, int fileSize, int fillerBytes) { ByteBuffer buffer = ByteBuffer.allocate(54); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put((byte) 0x42); //B buffer.put((byte) 0x4D); //M buffer.putInt(fileSize + 54); //total file size buffer.putInt(fileSize); //unofficial -- used to save the file size buffer.putInt(54); //pixel info offset buffer.putInt(40); //size of the bitmap info header buffer.putInt(width); //width buffer.putInt(height); //height buffer.putShort((short) 1); //number of color planes buffer.putShort((short) getBytesPerPixel()); //bytes per pixel buffer.putInt(0); //no compression buffer.putInt(fileSize); //size of the raw pixel array buffer.putInt(2835); //horizontal resolution buffer.putInt(2835); //vertical resolution buffer.putInt(0); //number of colors buffer.putInt(0); //important colors return new ByteArrayInputStream(buffer.array()); }
From source file:org.apache.hadoop.hbase.filter.SlicedRowFilter.java
/** * Serialize the filter//ww w . j av a 2 s. c o m */ @Override public byte[] toByteArray() throws IOException { // // Allocate buffer for the following data: // // count: 8 bytes // slicesLength: 4 bytes // nbounds: 4 bytes (this.bounds.length) // bounds: 4 * this.bounds.length // Size of range keys: 4 bytes (this.rangekeys.length) // slices: this.rangekeys // ByteBuffer bb = ByteBuffer.wrap(new byte[8 + 4 + 4 * this.bounds.length + 4 + 4 + this.rangekeys.length]) .order(ByteOrder.BIG_ENDIAN); bb.putLong(this.count); bb.putInt(this.slicesLength); bb.putInt(this.bounds.length); for (int i = 0; i < this.bounds.length; i++) { bb.putInt(this.bounds[i]); } bb.putInt(this.rangekeys.length); bb.put(this.rangekeys); return bb.array(); }
From source file:x10.x10rt.yarn.ApplicationMaster.java
private void processMessage(ByteBuffer msg, SocketChannel sc) { assert (msg.capacity() >= headerLength); msg.rewind(); // reset the buffer for reading from the beginning CTRL_MSG_TYPE type = CTRL_MSG_TYPE.values()[msg.getInt()]; int destination = msg.getInt(); final int source = msg.getInt(); int datalen = msg.getInt(); assert (datalen == msg.remaining()); LOG.info("Processing message of type " + type + ", size " + (headerLength + datalen) + " from place " + source);//from w w w . j a va 2 s .c o m /* System.err.print("Message contents:"); for (int i=0; i<msg.capacity(); i++) System.err.print(" "+Integer.toHexString(msg.get(i)).toUpperCase()); System.err.println(); */ switch (type) { case HELLO: { // read the port information, and update the record for this place assert (datalen == 4 && source < numRequestedContainers.get() && source >= 0); final CommunicationLink linkInfo; LOG.info("Getting link for place " + source); synchronized (links) { linkInfo = links.get(source); } linkInfo.port = ((msg.get() & 0xFF) << 8) | (msg.get() & 0xFF); // unsigned short in network order linkInfo.sc = sc; // check if there are pending port requests for this place if (linkInfo.pendingPortRequests != null) { // prepare response message String linkString = linkInfo.node.getHost() + ":" + linkInfo.port; byte[] linkBytes = linkString.getBytes(); // matches existing code. TODO: switch to UTF8 ByteBuffer response = ByteBuffer.allocateDirect(headerLength + linkBytes.length) .order(ByteOrder.nativeOrder()); response.putInt(CTRL_MSG_TYPE.PORT_RESPONSE.ordinal()); response.putInt(-1); response.putInt(source); response.putInt(linkBytes.length); response.put(linkBytes); // send response to each waiting place for (int place : linkInfo.pendingPortRequests) { response.putInt(4, place); // set the destination to the requesting place response.rewind(); // TODO: this code may get stuck here if the reciever isn't reading properly try { while (response.hasRemaining()) links.get(place).sc.write(response); } catch (IOException e) { LOG.warn("Unable to send out port response for place " + place + " to place " + source, e); } } linkInfo.pendingPortRequests = null; } LOG.info("HELLO from place " + source + " at port " + linkInfo.port); if (pendingKills != null && pendingKills.containsKey(source)) { int delay = pendingKills.remove(source); LOG.info("Scheduling a takedown of place " + source + " in " + delay + " seconds"); placeKiller.schedule(new Runnable() { @Override public void run() { LOG.info("KILLING CONTAINER FOR PLACE " + source); nodeManager.stopContainerAsync(linkInfo.container, linkInfo.node); } }, delay, TimeUnit.SECONDS); } } break; case GOODBYE: { try { CommunicationLink link = links.get(source); assert (link.pendingPortRequests == null); sc.close(); link.port = PORT_DEAD; } catch (IOException e) { LOG.warn("Error closing socket channel", e); } LOG.info("GOODBYE to place " + source); } break; case PORT_REQUEST: { LOG.info("Got PORT_REQUEST from place " + source + " for place " + destination); // check to see if we know the requested information CommunicationLink linkInfo = links.get(destination); if (linkInfo.port != PORT_UNKNOWN) { String linkString; if (linkInfo.port == PORT_DEAD) linkString = DEAD; else linkString = linkInfo.node.getHost() + ":" + linkInfo.port; LOG.info("Telling place " + source + " that place " + destination + " is at " + linkString); byte[] linkBytes = linkString.getBytes(); // matches existing code. TODO: switch to UTF8 ByteBuffer response = ByteBuffer.allocateDirect(headerLength + linkBytes.length) .order(ByteOrder.nativeOrder()); response.putInt(CTRL_MSG_TYPE.PORT_RESPONSE.ordinal()); response.putInt(source); response.putInt(destination); response.putInt(linkBytes.length); response.put(linkBytes); response.rewind(); // TODO: this code may get stuck here if the reciever isn't reading properly try { while (response.hasRemaining()) sc.write(response); } catch (IOException e) { LOG.warn("Unable to send out port response for place " + destination + " to place " + source, e); } } else { // port is not known. remember we have a place asking for it when it becomes available if (linkInfo.pendingPortRequests == null) linkInfo.pendingPortRequests = new ArrayList<Integer>(2); linkInfo.pendingPortRequests.add(source); LOG.info("Stashing PORT_REQUEST from place " + source + " for place " + destination + " until the answer is known"); } } break; case LAUNCH_REQUEST: { assert (datalen == 8); int numPlacesRequested = (int) msg.getLong(); int oldvalue = numRequestedContainers.getAndAdd((int) numPlacesRequested); // Send request for containers to RM for (int i = 0; i < numPlacesRequested; i++) { Resource capability = Resource.newInstance(memoryPerPlaceInMb, coresPerPlace); ContainerRequest request = new ContainerRequest(capability, null, null, Priority.newInstance(0)); LOG.info("Adding a new container request " + request.toString()); resourceManager.addContainerRequest(request); pendingRequests.add(request); } LOG.info("Requested an increase of " + numPlacesRequested + " places on top of the previous " + oldvalue + " places"); msg.rewind(); msg.putInt(CTRL_MSG_TYPE.LAUNCH_RESPONSE.ordinal()); msg.rewind(); try { while (msg.hasRemaining()) sc.write(msg); } catch (IOException e) { LOG.warn("Unable to send out launch response to place " + source, e); } } break; default: LOG.warn("unknown message type " + type); } LOG.info("Finished processing message of size " + (headerLength + datalen) + " from place " + source); }