public static void main(String[] arguments) { try { // read byte data into a byte buffer String data = "friends.dat"; FileInputStream inData = new FileInputStream(data); FileChannel inChannel = inData.getChannel(); long inSize = inChannel.size(); ByteBuffer source = ByteBuffer.allocate((int) inSize); inChannel.read(source, 0); source.position(0); System.out.println("Original byte data:"); for (int i = 0; source.remaining() > 0; i++) { System.out.print(source.get() + " "); } // convert byte data into character data source.position(0); Charset ascii = Charset.forName("US-ASCII"); CharsetDecoder toAscii = ascii.newDecoder(); CharBuffer destination = toAscii.decode(source); destination.position(0); System.out.println("\n\nNew character data:"); for (int i = 0; destination.remaining() > 0; i++) { System.out.print(destination.get()); } System.out.println(); } catch (FileNotFoundException fne) { System.out.println(fne.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } }
private void recv(SocketChannel sc, ClientInfo ci) throws IOException { ci.channel.read(ci.inBuf); ByteBuffer tmpBuf = ci.inBuf.duplicate(); tmpBuf.flip(); int bytesProcessed = 0; boolean doneLoop = false; while (!doneLoop) { byte b; try { b = tmpBuf.get(); } catch (BufferUnderflowException bue) { // Processed all data in buffer ci.inBuf.clear(); doneLoop = true; break; } switch (b) { case TypeServerConstants.WELCOME: bytesProcessed++; break; case TypeServerConstants.GET_STRING_REQUEST: bytesProcessed++; if (ci.outputPending) { // Client is backed up. We can't append to // the byte buffer because it's in the wrong // state. We could allocate another buffer // here and change our send method to know // about multiple buffers, but we'll just // assume that the client is dead break; } ci.outBuf.put(TypeServerConstants.GET_STRING_RESPONSE); ByteBuffer strBuf = encoder.encode(testString); ci.outBuf.putShort((short) strBuf.remaining()); ci.outBuf.put(strBuf); ci.outBuf.flip(); send(sc, ci); break; case TypeServerConstants.GET_STRING_RESPONSE: int startPos = tmpBuf.position(); try { int nBytes = tmpBuf.getInt(); byte[] buf = new byte[nBytes]; tmpBuf.get(buf); bytesProcessed += buf.length + 5; String s = new String(buf); // Send the string to the GUI break; } catch (BufferUnderflowException bue) { // Processed all available data ci.inBuf.position(ci.inBuf.position() + bytesProcessed); doneLoop = true; } break; } } }
/* * Return a ByteBuffer with "remaining" space to work. If you have to * reallocate the ByteBuffer, copy the existing info into the new buffer. */ protected void resizeRequestBB(int remaining) { if (requestBB.remaining() < remaining) { // Expand buffer for large request ByteBuffer bb = ByteBuffer.allocate(requestBB.capacity() * 2); requestBB.flip(); bb.put(requestBB); requestBB = bb; } }
private static WAVData readFromStream(AudioInputStream aIn) throws UnsupportedAudioFileException, IOException { ReadableByteChannel aChannel = Channels.newChannel(aIn); AudioFormat fmt = aIn.getFormat(); int numChannels = fmt.getChannels(); int bits = fmt.getSampleSizeInBits(); int format = AL_FORMAT_MONO8; if ((bits == 8) && (numChannels == 1)) { format = AL_FORMAT_MONO8; } else if ((bits == 16) && (numChannels == 1)) { format = AL_FORMAT_MONO16; } else if ((bits == 8) && (numChannels == 2)) { format = AL_FORMAT_STEREO8; } else if ((bits == 16) && (numChannels == 2)) { format = AL_FORMAT_STEREO16; } int freq = Math.round(fmt.getSampleRate()); int size = aIn.available(); ByteBuffer buffer = ByteBuffer.allocateDirect(size); while (buffer.remaining() > 0) { aChannel.read(buffer); } buffer.rewind(); // Must byte swap on big endian platforms // Thanks to swpalmer on javagaming.org forums for hint at fix if ((bits == 16) && (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)) { int len = buffer.remaining(); for (int i = 0; i < len; i += 2) { byte a = buffer.get(i); byte b = buffer.get(i + 1); buffer.put(i, b); buffer.put(i + 1, a); } } WAVData result = new WAVData(buffer, format, size, freq, false); aIn.close(); return result; }
public static void main(String args[]) throws Exception { final int ENOUGH_SIZE = 1024; final int SMALL_SIZE = 4; boolean isBlocked = true; int size = ENOUGH_SIZE; if (args.length > 0) { int opt = Integer.parseInt(args[0]); switch (opt) { case 1: isBlocked = true; size = ENOUGH_SIZE; break; case 2: isBlocked = true; size = SMALL_SIZE; break; case 3: isBlocked = false; size = ENOUGH_SIZE; break; case 4: isBlocked = false; size = SMALL_SIZE; break; } } DatagramChannel channel = DatagramChannel.open(); channel.configureBlocking(isBlocked); ByteBuffer buffer = ByteBuffer.allocate(size); DatagramSocket socket = channel.socket(); SocketAddress localAddr = new InetSocketAddress(8000); socket.bind(localAddr); while (true) { System.out.println("开始接收数据报"); SocketAddress remoteAddr = channel.receive(buffer); if (remoteAddr == null) { System.out.println("没有接收到数据报"); } else { buffer.flip(); System.out.println("接收到的数据报的大小为" + buffer.remaining()); } Thread.sleep(500); } }
public static void main(String args[]) throws Exception { FileInputStream fin = new FileInputStream("readandshow.txt"); FileChannel fc = fin.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); fc.read(buffer); buffer.flip(); int i = 0; while (buffer.remaining() > 0) { byte b = buffer.get(); System.out.println("Character " + i + ": " + ((char) b)); i++; } fin.close(); }
private synchronized DBMessage go(DBMessage msg, ByteDecoder decoder) throws IOException { if (_sock == null) _open(); { ByteBuffer out = msg.prepare(); while (out.remaining() > 0) _sock.write(out); } if (_pool != null) _pool._everWorked = true; if (decoder == null) return null; ByteBuffer response = decoder._buf; if (response.position() != 0) throw new IllegalArgumentException(); int read = 0; while (read < DBMessage.HEADER_LENGTH) read += _read(response); int len = response.getInt(0); if (len <= DBMessage.HEADER_LENGTH) throw new IllegalArgumentException("db sent invalid length: " + len); if (len > response.capacity()) throw new IllegalArgumentException( "db message size is too big (" + len + ") " + "max is (" + response.capacity() + ")"); response.limit(len); while (read < len) read += _read(response); if (read != len) throw new RuntimeException("something is wrong"); response.flip(); return new DBMessage(response); }
private int _read(ByteBuffer buf) throws IOException { int x = _in.read(buf.array(), buf.position(), buf.remaining()); if (x < 0) throw new IOException("connection to server closed unexpectedly"); buf.position(buf.position() + x); return x; }
private void readMessage(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) throws IOException { // note that socket has been used SocketEntry entry = (SocketEntry) sockets.get(incomingAddress); if (entry != null) { entry.used(); ByteBuffer readBuffer = entry.getReadBuffer(); if (readBuffer != null) { readChannel.read(readBuffer); if (readBuffer.hasRemaining()) { readChannel.register(selector, SelectionKey.OP_READ, entry); } else { dispatchMessage(incomingAddress, readBuffer, readBuffer.capacity()); } return; } } ByteBuffer byteBuffer = ByteBuffer.wrap(buf); byteBuffer.limit(messageLengthDecoder.getMinHeaderLength()); long bytesRead = readChannel.read(byteBuffer); if (logger.isDebugEnabled()) { logger.debug("Reading header " + bytesRead + " bytes from " + incomingAddress); } MessageLength messageLength = new MessageLength(0, Integer.MIN_VALUE); if (bytesRead == messageLengthDecoder.getMinHeaderLength()) { messageLength = messageLengthDecoder.getMessageLength(ByteBuffer.wrap(buf)); if (logger.isDebugEnabled()) { logger.debug("Message length is " + messageLength); } if ((messageLength.getMessageLength() > getMaxInboundMessageSize()) || (messageLength.getMessageLength() <= 0)) { logger.error( "Received message length " + messageLength + " is greater than inboundBufferSize " + getMaxInboundMessageSize()); synchronized (entry) { entry.getSocket().close(); logger.info("Socket to " + entry.getPeerAddress() + " closed due to an error"); } } else { byteBuffer.limit(messageLength.getMessageLength()); bytesRead += readChannel.read(byteBuffer); if (bytesRead == messageLength.getMessageLength()) { dispatchMessage(incomingAddress, byteBuffer, bytesRead); } else { byte[] message = new byte[byteBuffer.limit()]; byteBuffer.flip(); byteBuffer.get(message, 0, byteBuffer.limit() - byteBuffer.remaining()); entry.setReadBuffer(ByteBuffer.wrap(message)); } readChannel.register(selector, SelectionKey.OP_READ, entry); } } else if (bytesRead < 0) { logger.debug("Socket closed remotely"); sk.cancel(); readChannel.close(); TransportStateEvent e = new TransportStateEvent( DefaultTcpTransportMapping.this, incomingAddress, TransportStateEvent.STATE_DISCONNECTED_REMOTELY, null); fireConnectionStateChanged(e); } }