Package java.nio

Examples of java.nio.ByteBuffer


        }
        if(byteBuffer == null || ((byteBuffer.capacity() - byteBuffer.limit()) < size)) {
            byteBuffer = ByteBuffer.allocate(size);
        }
        byteBuffer.limit(byteBuffer.position() + size);
        ByteBuffer view = byteBuffer.slice();
        byteBuffer.position(byteBuffer.limit());
        return view;
    }
View Full Code Here


        b = Hex.decodeHex(dump.toCharArray());
    }
   
    public void testDecodeError() throws CharacterCodingException {
        CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
        ByteBuffer buffer = ByteBuffer.wrap(b);
       
        try {
            decoder.decode(buffer);
            fail("Must throw MalformedInputException");
        } catch (MalformedInputException e) {
View Full Code Here

        public int read(byte b[]) throws IOException {
            return read( b, 0, b.length );
        }

        public int read(byte b[], int off, int len) throws IOException {
            ByteBuffer buffer = ByteBuffer.wrap( b, off, len );
            return client.in.source().read( buffer );
        }
View Full Code Here

        public void write(byte b[]) throws IOException {
            write( b, 0, b.length );
        }

        public void write(byte b[], int off, int len) throws IOException {
            ByteBuffer buffer = ByteBuffer.wrap(b, off, len);
            while( buffer.hasRemaining() ) {
                int written = client.out.sink().write( buffer );
                if( written == 0 ) {
                    Thread.yield();
                }
            }
View Full Code Here

    } else {
      assert false : "Only mono or stereo is supported";
    }

    //read data into buffer
    ByteBuffer buffer = null;
    try {
      int available = ais.available();
      if(available <= 0) {
        available = ais.getFormat().getChannels() * (int) ais.getFrameLength() * ais.getFormat().getSampleSizeInBits() / 8;
      }
View Full Code Here

    return wavedata;
  }

  private static ByteBuffer convertAudioBytes(byte[] audio_bytes, boolean two_bytes_data) {
    ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
    dest.order(ByteOrder.nativeOrder());
    ByteBuffer src = ByteBuffer.wrap(audio_bytes);
    src.order(ByteOrder.LITTLE_ENDIAN);
    if (two_bytes_data) {
      ShortBuffer dest_short = dest.asShortBuffer();
      ShortBuffer src_short = src.asShortBuffer();
      while (src_short.hasRemaining())
        dest_short.put(src_short.get());
    } else {
      while (src.hasRemaining())
        dest.put(src.get());
    }
    dest.rewind();
    return dest;
  }
View Full Code Here

   
      int  pos_before  = read_insert.position();
     
      for (int i=array_offset;i<array_offset+length;i++){
       
        ByteBuffer  buffer = buffers[i];
       
        int  space = buffer.remaining();
       
        if ( space > 0 ){
         
          if ( space < read_insert.remaining()){
           
            int  old_limit = read_insert.limit();
           
            read_insert.limit( read_insert.position() + space );
           
            buffer.put( read_insert );

            read_insert.limit( old_limit );
           
          }else{
           
            buffer.put( read_insert );
          }
         
          if ( !read_insert.hasRemaining()){
                     
            break;
View Full Code Here

        {
          public boolean
          selectSuccess(
            VirtualChannelSelector selector, SocketChannel sc, Object attachment)
          {
            ByteBuffer  buffer = ByteBuffer.allocate(1024);
           
            try{
              long  len = filter.read( new ByteBuffer[]{ buffer }, 0, 1 );
           
              byte[]  data = new byte[buffer.position()];
             
              buffer.flip();
             
              buffer.get( data );
             
              System.out.println( str + ": " + new String(data));
             
              return( len > 0 );
             
View Full Code Here

    }catch( UnsupportedEncodingException e ){
     
      bytes = data.getBytes();
    }
       
    final ByteBuffer bb = ByteBuffer.wrap( bytes );
   
    try{
      transport.write( bb, false );
     
      if ( bb.remaining() > 0 ){
       
        transport.registerForWriteSelects(
          new TransportHelper.selectListener()
          {
              public boolean
              selectSuccess(
                TransportHelper  helper,
                Object       attachment )
              {
                try{
                  int written = helper.write( bb, false );
                 
                  if ( bb.remaining() > 0 ){
                 
                    helper.registerForWriteSelects( this, null );
                   
                  }else{
                   
View Full Code Here

 
  public byte[] getChunkPayload() {
   
    byte[] contentPayload = getContentPayload();
    int length = contentPayload.length;
    ByteBuffer buffer = ByteBuffer.allocate(length + 12);
    buffer.putInt(length);
    buffer.put(type);
    buffer.put(contentPayload);
   
    buffer.position(4);
    buffer.limit(length + 8);
   
    long crc = crc(buffer);
    buffer.limit(length + 12);
    buffer.putInt((int)crc);
   
    buffer.position(0);
    return buffer.array();
   
  }
View Full Code Here

TOP

Related Classes of java.nio.ByteBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.