List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:com.example.IdGenerator.java
public long generateLong() { final LocalDateTime now = LocalDateTime.now(clock); final Duration duration = Duration.between(BASE, now); final int high = (int) duration.getSeconds(); final int low = duration.getNano(); final byte[] hbs = ByteBuffer.allocate(4).putInt(high).array(); final byte[] lbs = ByteBuffer.allocate(4).putInt(low).array(); final byte[] bytes = new byte[8]; System.arraycopy(hbs, 0, bytes, 0, 4); System.arraycopy(lbs, 0, bytes, 4, 4); final ByteBuffer buffer = ByteBuffer.allocate(8).put(bytes, 0, 8); buffer.flip(); return buffer.getLong(); }
From source file:org.apache.airavata.gfac.core.GFacUtils.java
public static long bytesToLong(byte[] bytes) { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.put(bytes);/* ww w . ja v a 2s . c o m*/ buffer.flip();//need flip return buffer.getLong(); }
From source file:org.apache.tez.test.TestOutput.java
@Override public List<Event> close() throws Exception { LOG.info("Sending data movement event with value: " + output); ByteBuffer result = ByteBuffer.allocate(4).putInt(output); result.flip(); List<Event> events = Lists.newArrayListWithCapacity(getNumPhysicalOutputs()); for (int i = 0; i < getNumPhysicalOutputs(); i++) { DataMovementEvent event = DataMovementEvent.create(i, result); events.add(event);/*from ww w .j a va 2 s .c o m*/ } return events; }
From source file:MainClass.java
public void run() { try {// w w w . jav a 2 s. co m ByteBuffer buffer = ByteBuffer.allocate(4); buffer.putInt(this.howMany); buffer.flip(); while (buffer.hasRemaining()) out.write(buffer); for (int i = 0; i < howMany; i++) { byte[] data = new BigInteger(Integer.toString(i)).toByteArray(); buffer = ByteBuffer.allocate(4 + data.length); buffer.putInt(data.length); buffer.put(data); buffer.flip(); while (buffer.hasRemaining()) out.write(buffer); } out.close(); System.err.println("Closed"); } catch (IOException ex) { System.err.println(ex); } }
From source file:com.openteach.diamond.network.waverider.command.MasterGreetCommandHandler.java
private Command makeGreetCommand(Session session) { String hello = new StringBuilder("Hello slave:").append(session.getSlaveWorker().getIp()).toString(); ByteBuffer buffer = ByteBuffer.allocate(hello.getBytes().length); buffer.put(hello.getBytes());/* w w w . j a v a 2 s . c om*/ buffer.flip(); Command command = new Command(1L, buffer); return command; }
From source file:com.jim.im.broker.config.ImBrokerMessageListener.java
private PublishMessage createPublishMessage(ImMessage imMessage) throws UnsupportedEncodingException { PublishMessage publishMessage = new PublishMessage(); publishMessage.setTopicName(imMessage.getTopicName()); publishMessage.setQos(AbstractMessage.QOSType.MOST_ONE); ByteBuffer messageId = ByteBuffer.wrap(imMessage.getId().toString().getBytes()); messageId.flip(); publishMessage.setPayload(messageId); return publishMessage; }
From source file:RegexProperties.java
public void load(FileInputStream inStream) throws IOException, PatternSyntaxException { FileChannel fc = inStream.getChannel(); ByteBuffer bb = ByteBuffer.allocate((int) fc.size()); fc.read(bb);//from ww w . j ava2s.co m bb.flip(); String fileContent = new String(bb.array()); Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE); Matcher matcher = pattern.matcher(fileContent); while (matcher.find()) { String line = matcher.group(1); if (line != null && !"".equals(line.trim()) && !line.startsWith("#") && !line.startsWith("!")) { String keyValue[] = null; if (line.indexOf("=") > 0) keyValue = line.split("=", 2); else keyValue = line.split(":", 2); if (keyValue != null) { super.put(keyValue[0].trim(), keyValue[1]); } } } fc = null; bb = null; }
From source file:com.gamesalutes.utils.ByteUtils.java
/** * Reads all the bytes from the given input stream and stores them in the specified buffer. * If the input buffer is <code>null</code> or does not have the capacity to store all the input, a * new buffer is created and returned. The input stream is closed regardless of whether an * <code>IOException</code> is thrown. * /*from ww w. j av a2 s. c om*/ * * @param in the <code>InputStream</code> to read * @param buf a <code>ByteBuffer</code> to use for storage or <code>null</code> to just allocate a new one * If <code>buf</code> is not large enough it will be expanded using {@link #growBuffer(ByteBuffer, int)} * @return the buffer containing the read data * @throws IOException */ public static ByteBuffer readBytes(InputStream in, ByteBuffer buf) throws IOException { try { if (buf == null) buf = ByteBuffer.allocate(READ_BUFFER_SIZE); // note the input position int startPos = buf.position(); byte[] tmp = new byte[NETWORK_BYTE_SIZE]; int read; // read until end of file while ((read = in.read(tmp)) > 0) { if (buf.remaining() < read) { buf = ByteUtils.growBuffer(buf, buf.limit() + (read - buf.remaining())); } buf.put(tmp, 0, read); } buf.flip(); // reset starting position to be that of input buffer buf.position(startPos); return buf; } finally { MiscUtils.closeStream(in); } }
From source file:byps.BWire.java
/** * Reads a ByteBuffer from an InputStream * Closes the InputStream.//from w w w . j a va 2s . c o m * @param is * @return * @throws IOException */ public static ByteBuffer bufferFromStream(InputStream is, Boolean gzip) throws IOException { if (is == null) return null; try { ByteBuffer ibuf = ByteBuffer.allocate(10 * 1000); if (gzip != null) { if (gzip) { is = new GZIPInputStream(is); } } else { if (!is.markSupported()) is = new BufferedInputStream(is, 2); is.mark(2); int magic = is.read() | (is.read() << 8); is.reset(); if (magic == GZIPInputStream.GZIP_MAGIC) { is = new GZIPInputStream(is); } } ReadableByteChannel rch = Channels.newChannel(is); while (rch.read(ibuf) != -1) { if (ibuf.remaining() == 0) { ByteBuffer nbuf = ByteBuffer.allocate(ibuf.capacity() * 2); ibuf.flip(); nbuf.put(ibuf); ibuf = nbuf; } } ibuf.flip(); return ibuf; } finally { is.close(); } }
From source file:idgs.client.SocketChannelHandler.java
/** * handle connect/* w w w. j a va2 s. c om*/ * @throws IOException */ public void onConnected(SocketChannel channel) throws IOException { ClientLogin login = new ClientLogin(); ByteBuffer buffer = login.toBuffer(); buffer.flip(); int writeClientLoginSize = channel.write(buffer); if (writeClientLoginSize > 0) { log.debug("writer client login content: " + new String(login.toString())); log.debug("byte size: " + writeClientLoginSize + ", order by " + RpcBuffer.DEFAULT_BYTE_ORDER.toString() + " bytes: " + Arrays.toString(buffer.array())); } }