Java Code Examples for java.nio.ByteBuffer#putChar()
The following examples show how to use
java.nio.ByteBuffer#putChar() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelsTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testNewChannelOutputStream_inputNull() throws IOException { int writeres = this.testNum; ByteBuffer writebuf = ByteBuffer.allocate(this.writebufSize); for (int val = 0; val < this.writebufSize / 2; val++) { writebuf.putChar((char) (val + 64)); } this.fouts = null; try { WritableByteChannel rbChannel = Channels.newChannel(this.fouts); writeres = rbChannel.write(writebuf); assertEquals(0, writeres); writebuf.flip(); writeres = rbChannel.write(writebuf); fail("Should throw NPE."); } catch (NullPointerException expected) { } }
Example 2
Source File: Block.java From Bitcoin with Apache License 2.0 | 6 votes |
public void toBuffer(ByteBuffer buffer) { final byte[] fBytes = from.getBytes(); buffer.putInt(fBytes.length); buffer.put(fBytes); buffer.putChar(getBoolean(confirmed)); buffer.putInt(numberOfZeros); buffer.putInt(nonce); buffer.putInt(blockLength); buffer.putInt(prev.length); buffer.put(prev); buffer.putInt(hash.length); buffer.put(hash); buffer.putInt(transactions.length); for (Transaction t : transactions) { buffer.putInt(t.getBufferLength()); t.toBuffer(buffer); } }
Example 3
Source File: AddressUtils.java From bt with Apache License 2.0 | 6 votes |
public static byte[] packAddress(InetSocketAddress addr) { byte[] result = null; if(addr.getAddress() instanceof Inet4Address) { result = new byte[6]; } if(addr.getAddress() instanceof Inet6Address) { result = new byte[18]; } ByteBuffer buf = ByteBuffer.wrap(result); buf.put(addr.getAddress().getAddress()); buf.putChar((char)(addr.getPort() & 0xffff)); return result; }
Example 4
Source File: MessageCoder.java From redkale with Apache License 2.0 | 6 votes |
public static byte[] getBytes(final Map<String, String> map) { if (map == null || map.isEmpty()) return new byte[2]; final AtomicInteger len = new AtomicInteger(2); map.forEach((key, value) -> { len.addAndGet(2 + (key == null ? 0 : Utility.encodeUTF8Length(key))); len.addAndGet(4 + (value == null ? 0 : Utility.encodeUTF8Length(value))); }); final byte[] bs = new byte[len.get()]; final ByteBuffer buffer = ByteBuffer.wrap(bs); buffer.putChar((char) map.size()); map.forEach((key, value) -> { putShortString(buffer, key); putLongString(buffer, value); }); return bs; }
Example 5
Source File: StringTool.java From terracotta-platform with Apache License 2.0 | 6 votes |
/** * Appends the non-encoded, byte-by-byte representation of a {@code String} to the {@code ByeBuffer} * provided. The buffer's position is advanced by the number of bytes required by the representation * (including the length). The bytes are <b>not</b> locale-encoded. * * @param buffer the {@code ByteBuffer} into which {@code str} is encoded * @param str the {@code String} to encode * @param strLength the length of {@code str} * @throws BufferOverflowException if {@code buffer} is too small for the UTF-encoded {@code str} * @throws ReadOnlyBufferException if {@code buffer} is read-only */ private static void putNonEncoded(final ByteBuffer buffer, final String str, final int strLength) throws BufferOverflowException, ReadOnlyBufferException { final int byteLength = strLength * 2; buffer.put((byte) 0x02).putLong(byteLength); if (byteLength > buffer.remaining()) { throw new BufferOverflowException(); } final char[] slice = new char[MAX_SLICE_LENGTH]; for (int offset = 0; offset < strLength; offset += MAX_SLICE_LENGTH) { final int sliceLength = Math.min(MAX_SLICE_LENGTH, strLength - offset); str.getChars(offset, offset + sliceLength, slice, 0); for (int i = 0; i < sliceLength; i++) { buffer.putChar(slice[i]); } } }
Example 6
Source File: PrimitiveConstant.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void serialize(ByteBuffer buffer) { switch (getJavaKind()) { case Byte: case Boolean: buffer.put((byte) primitive); break; case Short: buffer.putShort((short) primitive); break; case Char: buffer.putChar((char) primitive); break; case Int: buffer.putInt(asInt()); break; case Long: buffer.putLong(asLong()); break; case Float: buffer.putFloat(asFloat()); break; case Double: buffer.putDouble(asDouble()); break; default: throw new IllegalArgumentException("unexpected kind " + getJavaKind()); } }
Example 7
Source File: MemoryTest.java From incubator-datasketches-memory with Apache License 2.0 | 5 votes |
@Test public void wrapBigEndianAsLittle() { ByteBuffer bb = ByteBuffer.allocate(64); bb.putChar(0, (char)1); //as BE Memory mem = Memory.wrap(bb, ByteOrder.LITTLE_ENDIAN); assertEquals(mem.getChar(0), 256); }
Example 8
Source File: IpmiAlgorithmUtils.java From ipmi4j with Apache License 2.0 | 5 votes |
public static void toWireUnchecked(@Nonnull ByteBuffer buffer, @Nonnull IpmiAlgorithm algorithm) { buffer.put(algorithm.getPayloadType()); buffer.putChar((char) 0); // reserved buffer.put(getWireLength()); buffer.put(algorithm.getCode()); buffer.put((byte) 0); // reserved buffer.putChar((char) 0); // reserved }
Example 9
Source File: AsfOpenSessionResponseData.java From ipmi4j with Apache License 2.0 | 5 votes |
@Override protected void toWireData(ByteBuffer buffer) { buffer.put(status.getCode()); buffer.putChar((char) 0); // Reserved, zero buffer.putInt(getConsoleSessionId()); buffer.putInt(getClientSessionId()); authenticationAlgorithm.toWire(buffer); integrityAlgorithm.toWire(buffer); }
Example 10
Source File: AlphabeticalSerializer.java From VSerializer with GNU General Public License v2.0 | 5 votes |
private byte[] toBytes(char[] chars) { ByteBuffer byteBuffer = ByteBuffer.allocate(chars.length*2); for (char c : chars) { byteBuffer.putChar(c); } return byteBuffer.array(); }
Example 11
Source File: PayloadTests.java From packer-ng-plugin with Apache License 2.0 | 5 votes |
public void testBufferWrite() throws IOException { File f = newTestFile(); byte[] string = "Hello".getBytes(); ByteBuffer in = ByteBuffer.allocate(1024); in.order(ByteOrder.LITTLE_ENDIAN); in.putInt(123); in.putChar('z'); in.putShort((short) 2017); in.putFloat(3.1415f); in.putLong(9876543210L); in.putDouble(3.14159265); in.put((byte) 5); in.put(string); in.flip(); // important // TestUtils.showBuffer(in); Support.writeBlock(f, 0x123456, in); ByteBuffer out = Support.readBlock(f, 0x123456); assertNotNull(out); // TestUtils.showBuffer(out); assertEquals(123, out.getInt()); assertEquals('z', out.getChar()); assertEquals(2017, out.getShort()); assertEquals(3.1415f, out.getFloat()); assertEquals(9876543210L, out.getLong()); assertEquals(3.14159265, out.getDouble()); assertEquals((byte) 5, out.get()); byte[] so = new byte[string.length]; out.get(so); assertTrue(TestUtils.sameBytes(string, so)); checkApkVerified(f); }
Example 12
Source File: UuidUtil.java From Rhombus with MIT License | 5 votes |
/** * Generate a type 3 namespace uuid from an integer namespace and name * Our primary objective is to store and retrieve our name and namespace, and we don't care about collisions, * so we'll just shove the name and namespace values straight in rather than hashing them * * We will allow 4 bytes for the namespace and 8 bytes for the name. To avoid conflicts with reserved * version/variant bits, we will put the namespace data in the time_low field, 6 least significant bytes of the name data * in the node field, and the remaining 2 most significant name bytes in time_mid * * @param namespace Integer representing the namespace * @param name Long representing the name * @return Type 3 UUID built from the namespace and name */ public static UUID namespaceUUID(Integer namespace, Long name) { //Create our msb and lsb return buffers ByteBuffer msb = ByteBuffer.allocate(8); msb.order(ByteOrder.BIG_ENDIAN); ByteBuffer lsb = ByteBuffer.allocate(8); msb.order(ByteOrder.BIG_ENDIAN); // Insert the 4 byte namespace into the time_low field msb.putInt(0, namespace); // Slice off the most significant two bytes of name char nameHigh = (char)(name >>> 48); // Push the 2 MSB bytes of name into the time_mid field msb.putChar(4, nameHigh); //Set the four most significant bits of the time_hi_and_version field to the 4 bit version number //(00110000 = 48) msb.put(7, (byte) 48); // Grab the least significant six bytes of name char nameMid = (char)(name >>> 32); int nameLow = (int)(long)name; // Shove them into the node field lsb.putChar(2, nameMid); lsb.putInt(4, nameLow); //Set the two most significant bits to 01 (01000000 = 64) lsb.put(0, (byte)64); return new UUID(msb.getLong(), lsb.getLong()); }
Example 13
Source File: CommanderPacket.java From crazyflie-android-client with GNU General Public License v2.0 | 5 votes |
@Override protected void serializeData(ByteBuffer buffer) { buffer.putFloat(mRoll); buffer.putFloat(-mPitch); //invert axis buffer.putFloat(mYaw); buffer.putChar(mThrust); }
Example 14
Source File: HashTableTestUtils.java From HaloDB with Apache License 2.0 | 5 votes |
public void serialize(Integer s, ByteBuffer buf) { buf.put((byte)(1 & 0xff)); buf.putChar('A'); buf.putDouble(42.42424242d); buf.putFloat(11.111f); buf.putInt(s); buf.putLong(Long.MAX_VALUE); buf.putShort((short)(0x7654 & 0xFFFF)); buf.put(dummyByteArray); }
Example 15
Source File: AbstractNonStreamingHashFunction.java From exonum-java-binding with Apache License 2.0 | 5 votes |
@Override public HashCode hashUnencodedChars(CharSequence input) { int len = input.length(); ByteBuffer buffer = ByteBuffer.allocate(len * 2).order(ByteOrder.LITTLE_ENDIAN); for (int i = 0; i < len; i++) { buffer.putChar(input.charAt(i)); } return hashBytes(buffer.array()); }
Example 16
Source File: MutableRoaringArray.java From RoaringBitmap with Apache License 2.0 | 4 votes |
/** * Serialize. * * The current bitmap is not modified. * * @param buffer the ByteBuffer to write to */ @Override public void serialize(ByteBuffer buffer) { ByteBuffer buf = buffer.order() == LITTLE_ENDIAN ? buffer : buffer.slice().order(LITTLE_ENDIAN); int startOffset; boolean hasrun = hasRunCompression(); if (hasrun) { buf.putInt(SERIAL_COOKIE | ((size - 1) << 16)); int offset = buf.position(); for (int i = 0; i < size; i += 8) { int runMarker = 0; for (int j = 0; j < 8 && i + j < size; ++j) { if (values[i + j] instanceof MappeableRunContainer) { runMarker |= (1 << j); } } buf.put((byte)runMarker); } int runMarkersLength = buf.position() - offset; if (this.size < NO_OFFSET_THRESHOLD) { startOffset = 4 + 4 * this.size + runMarkersLength; } else { startOffset = 4 + 8 * this.size + runMarkersLength; } } else { // backwards compatibility buf.putInt(SERIAL_COOKIE_NO_RUNCONTAINER); buf.putInt(size); startOffset = 4 + 4 + 4 * this.size + 4 * this.size; } for (int k = 0; k < size; ++k) { buf.putChar(this.keys[k]); buf.putChar((char) (this.values[k].getCardinality() - 1)); } if ((!hasrun) || (this.size >= NO_OFFSET_THRESHOLD)) { // writing the containers offsets for (int k = 0; k < this.size; ++k) { buf.putInt(startOffset); startOffset = startOffset + this.values[k].getArraySizeInBytes(); } } for (int k = 0; k < size; ++k) { values[k].writeArray(buf); } if (buf != buffer) { buffer.position(buffer.position() + buf.position()); } }
Example 17
Source File: AdminPForge.java From L2jOrg with GNU General Public License v3.0 | 4 votes |
private boolean write(byte b, String string, ByteBuffer buf) { if ((b == 'C') || (b == 'c')) { buf.put(Integer.decode(string).byteValue()); return true; } else if ((b == 'D') || (b == 'd')) { buf.putInt(Integer.decode(string)); return true; } else if ((b == 'H') || (b == 'h')) { buf.putShort(Integer.decode(string).shortValue()); return true; } else if ((b == 'F') || (b == 'f')) { buf.putDouble(Double.parseDouble(string)); return true; } else if ((b == 'S') || (b == 's')) { final int len = string.length(); for (int i = 0; i < len; i++) { buf.putChar(string.charAt(i)); } buf.putChar('\000'); return true; } else if ((b == 'B') || (b == 'b') || (b == 'X') || (b == 'x')) { buf.put(new BigInteger(string).toByteArray()); return true; } else if ((b == 'Q') || (b == 'q')) { buf.putLong(Long.decode(string)); return true; } return false; }
Example 18
Source File: LogItem.java From SimpleOpenVpn-Android with Apache License 2.0 | 4 votes |
public byte[] getMarschaledBytes() throws UnsupportedEncodingException, BufferOverflowException { ByteBuffer bb = ByteBuffer.allocate(16384); bb.put((byte) 0x0); //version bb.putLong(logtime); //8 bb.putInt(mVerbosityLevel); //4 bb.putInt(mLevel.getInt()); bb.putInt(mRessourceId); if (mMessage == null || mMessage.length() == 0) { bb.putInt(0); } else { marschalString(mMessage, bb); } if (mArgs == null || mArgs.length == 0) { bb.putInt(0); } else { bb.putInt(mArgs.length); for (Object o : mArgs) { if (o instanceof String) { bb.putChar('s'); marschalString((String) o, bb); } else if (o instanceof Integer) { bb.putChar('i'); bb.putInt((Integer) o); } else if (o instanceof Float) { bb.putChar('f'); bb.putFloat((Float) o); } else if (o instanceof Double) { bb.putChar('d'); bb.putDouble((Double) o); } else if (o instanceof Long) { bb.putChar('l'); bb.putLong((Long) o); } else if (o == null) { bb.putChar('0'); } else { VpnStatus.logDebug("Unknown object for LogItem marschaling " + o); bb.putChar('s'); marschalString(o.toString(), bb); } } } int pos = bb.position(); bb.rewind(); return Arrays.copyOf(bb.array(), pos); }
Example 19
Source File: LogItem.java From Cake-VPN with GNU General Public License v2.0 | 4 votes |
public byte[] getMarschaledBytes() throws UnsupportedEncodingException, BufferOverflowException { ByteBuffer bb = ByteBuffer.allocate(16384); bb.put((byte) 0x0); //version bb.putLong(logtime); //8 bb.putInt(mVerbosityLevel); //4 bb.putInt(mLevel.getInt()); bb.putInt(mRessourceId); if (mMessage == null || mMessage.length() == 0) { bb.putInt(0); } else { marschalString(mMessage, bb); } if (mArgs == null || mArgs.length == 0) { bb.putInt(0); } else { bb.putInt(mArgs.length); for (Object o : mArgs) { if (o instanceof String) { bb.putChar('s'); marschalString((String) o, bb); } else if (o instanceof Integer) { bb.putChar('i'); bb.putInt((Integer) o); } else if (o instanceof Float) { bb.putChar('f'); bb.putFloat((Float) o); } else if (o instanceof Double) { bb.putChar('d'); bb.putDouble((Double) o); } else if (o instanceof Long) { bb.putChar('l'); bb.putLong((Long) o); } else if (o == null) { bb.putChar('0'); } else { VpnStatus.logDebug("Unknown object for LogItem marschaling " + o); bb.putChar('s'); marschalString(o.toString(), bb); } } } int pos = bb.position(); bb.rewind(); return Arrays.copyOf(bb.array(), pos); }
Example 20
Source File: Cloader.java From crazyflie-android-client with GNU General Public License v2.0 | 4 votes |
/** * Read back a flash page from the Crazyflie and return it */ //def read_flash(self, addr=0xFF, page=0x00): public byte[] readFlash(int addr, int page) { ByteBuffer buff = null; Target target = this.mTargets.get(addr); if (target != null) { int pageSize = target.getPageSize(); buff = ByteBuffer.allocate(pageSize + 1); for (int i = 0; i < Math.ceil(pageSize / 25.0); i++) { CrtpPacket replyPk = null; int retryCounter = 5; while (retryCounter >= 0) { ByteBuffer bb = ByteBuffer.allocate(6).order(ByteOrder.LITTLE_ENDIAN); bb.put((byte) addr); bb.put((byte) READ_FLASH); bb.putChar((char) page); bb.putChar((char) (i*25)); sendBootloaderPacket(bb.array()); //System.out.println("ByteString send: " + getHexString(pk.getPayload()) + " " + UsbLinkJava.getByteString(pk.getPayload())); //TODO: why is this different than in Python? //does it have something to do with the queue size?? //yes, the queue is filled with empty packets //how can this be avoided? while(!isBootloaderReplyPacket(replyPk, addr, READ_FLASH)) { replyPk = this.mDriver.receivePacket(10); } if (replyPk != null) { break; } retryCounter--; } if (retryCounter < 0) { mLogger.debug("Returning null..."); return new byte[0]; } else { buff.put(replyPk.getPayload(), 6, replyPk.getPayload().length - 6); } } } //return buff[0:page_size] # For some reason we get one byte extra here... //-> because of the ceil function? return buff.array(); }