Java Code Examples for java.nio.ByteBuffer.putChar()

The following are Jave code examples for showing how to use putChar() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: AndroidSDK2.0   File: ByteConverter.java   View Source Code Vote up 6 votes
/**
 * Char array tobyte byte [ ].
 *
 * @param values the values
 * @return the byte [ ]
 */
public static byte[] charArrayTobyte(char[] values)
{
	int byteLength = values.length * 2;
	
	ByteBuffer bf = ByteBuffer.allocate( byteLength );
    
	for ( char v : values )
	{
		bf.putChar( v );
	}
	
	byte[] byteArray = bf.array();
	byte[] returnbyte = new byte[byteLength];
	
    for(int i=0;i < byteLength; i++)
    {
        returnbyte[i] = byteArray[(byteLength-1)-i];
    }
    
    return returnbyte;
}
 
Example 2
Project: JDA-Audio   File: AudioPacket.java   View Source Code Vote up 6 votes
public AudioPacket(char seq, int timestamp, int ssrc, byte[] encodedAudio)
{
    this.seq = seq;
    this.ssrc = ssrc;
    this.timestamp = timestamp;
    this.encodedAudio = encodedAudio;

    ByteBuffer buffer = ByteBuffer.allocate(RTP_HEADER_BYTE_LENGTH + encodedAudio.length);
    buffer.put(RTP_VERSION_PAD_EXTEND_INDEX, RTP_VERSION_PAD_EXTEND);   //0
    buffer.put(RTP_PAYLOAD_INDEX, RTP_PAYLOAD_TYPE);                    //1
    buffer.putChar(SEQ_INDEX, seq);                                     //2 - 3
    buffer.putInt(TIMESTAMP_INDEX, timestamp);                          //4 - 7
    buffer.putInt(SSRC_INDEX, ssrc);                                    //8 - 11
    System.arraycopy(encodedAudio, 0, buffer.array(), RTP_HEADER_BYTE_LENGTH, encodedAudio.length);//12 - n
    this.rawPacket = buffer.array();

}
 
Example 3
Project: jvb   File: CircularSampleBuffer.java   View Source Code Vote up 5 votes
public void writeSample(char left, char right) {
    if (writeAvailableBytes() < 4) {
        throw new BufferOverflowException();
    }
    ByteBuffer bb = ByteBuffer.wrap(buffer);
    bb.order(order);
    bb.putChar(writePos, left);
    bb.putChar(writePos + 2, right);
    writePos += 4;
    readAvailable += 4;
    if (writePos >= buffer.length) {
        writePos = 0;
    }
}
 
Example 4
Project: openjdk-jdk10   File: PrimitiveConstant.java   View Source Code Vote up 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 5
Project: openjdk-jdk10   File: ByteBufferTest.java   View Source Code Vote up 5 votes
void putOne(ByteBuffer b, PrimitiveType t) {
    switch (t) {
    case BYTE: b.put((byte)0); break;
    case CHAR: b.putChar('0'); break;
    case SHORT: b.putShort((short)0); break;
    case INT: b.putInt(0); break;
    case LONG: b.putLong(0); break;
    case FLOAT: b.putFloat(0); break;
    case DOUBLE: b.putDouble(0); break;
    }
}
 
Example 6
Project: openjdk-jdk10   File: ByteBufferTest.java   View Source Code Vote up 5 votes
void putOne(ByteBuffer b, PrimitiveType t, int index) {
    switch (t) {
    case BYTE: b.put(index, (byte)0); break;
    case CHAR: b.putChar(index, '0'); break;
    case SHORT: b.putShort(index, (short)0); break;
    case INT: b.putInt(index, 0); break;
    case LONG: b.putLong(index, 0); break;
    case FLOAT: b.putFloat(index, 0); break;
    case DOUBLE: b.putDouble(index, 0); break;
    }
}
 
Example 7
Project: openjdk-jdk10   File: Frame.java   View Source Code Vote up 5 votes
void write(ByteBuffer buffer) {
    buffer.putChar(firstChar);
    if (payloadLen >= 126) {
        if (payloadLen < 65536) {
            buffer.putChar((char) payloadLen);
        } else {
            buffer.putLong(payloadLen);
        }
    }
    if (mask) {
        buffer.putInt(maskingKey);
    }
}
 
Example 8
Project: samba-documents-provider   File: BroadcastUtils.java   View Source Code Vote up 5 votes
/**
 * Generates a NetBIOS name query request.
 * https://tools.ietf.org/html/rfc1002
 * Section 4.2.12
 */
static byte[] createPacket(int transId) {
  ByteBuffer os = ByteBuffer.allocate(50);

  char broadcastFlag = 0x0010;
  char questionCount = 1;
  char answerResourceCount = 0;
  char authorityResourceCount = 0;
  char additionalResourceCount = 0;

  os.putChar((char) transId);
  os.putChar(broadcastFlag);
  os.putChar(questionCount);
  os.putChar(answerResourceCount);
  os.putChar(authorityResourceCount);
  os.putChar(additionalResourceCount);

  // Length of name. 16 bytes of name encoded to 32 bytes.
  os.put((byte) 0x20);

  // '*' character encodes to 2 bytes.
  os.put((byte) 0x43);
  os.put((byte) 0x4b);

  // Write the remaining 15 nulls which encode to 30* 0x41
  for (int i = 0; i < 30; i++) {
    os.put((byte) 0x41);
  }

  // Length of next segment.
  os.put((byte) 0);

  // Question type: Node status
  os.putChar((char) 0x21);

  // Question class: Internet
  os.putChar((char) 0x01);

  return os.array();
}
 
Example 9
Project: debug   File: RT.java   View Source Code Vote up 4 votes
public static ByteBuffer putChar(ByteBuffer buf, char value) {
    buf.putChar(value);
    writeByteBuffer(buf);
    return buf;
}
 
Example 10
Project: debug   File: RT.java   View Source Code Vote up 4 votes
public static ByteBuffer putChar(ByteBuffer buf, int index, char value) {
    buf.putChar(index, value);
    writeByteBuffer(buf);
    return buf;
}
 
Example 11
Project: Cybernet-VPN   File: LogItem.java   View Source Code Vote up 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 12
Project: L2J-Global   File: AdminPForge.java   View Source Code Vote up 4 votes
private boolean write(byte b, String string, ByteBuffer buf)
{
	if ((b == 'C') || (b == 'c'))
	{
		buf.put(Byte.decode(string));
		return true;
	}
	else if ((b == 'D') || (b == 'd'))
	{
		buf.putInt(Integer.decode(string));
		return true;
	}
	else if ((b == 'H') || (b == 'h'))
	{
		buf.putShort(Short.decode(string));
		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;
}