nioutils.java
来自「一个MMORPG手机游戏的服务器端程序源代码」· Java 代码 · 共 133 行
JAVA
133 行
package zsw_mmorpg.common;import java.nio.*;import java.nio.channels.*;import java.io.UnsupportedEncodingException;/** * NIOUtils.java * * NIO channels * * @author <a href="mailto:shiwei@raymobile.com">朱世伟</a> * @version 1.0 */public class NIOUtils { /** * 写之前处理下数据包格式 */ public static void prepBuffer(GameEvent event, ByteBuffer writeBuffer) { // write header writeBuffer.clear();// writeBuffer.putInt(0);// if (event.getGameName() != null)// writeBuffer.putInt(event.getGameName().hashCode());// else// writeBuffer.putInt(0); int sizePos = writeBuffer.position(); writeBuffer.putInt(0);// placeholder for payload size // write event int payloadSize = event.write(writeBuffer); // insert the payload size in the placeholder spot writeBuffer.putInt(sizePos, payloadSize); // prepare for a channel.write writeBuffer.flip(); } /** * 往给定通道发送数据 */ public static void channelWrite(SocketChannel channel, ByteBuffer writeBuffer) { long nbytes = 0; long toWrite = writeBuffer.remaining(); byte[] array = new byte[0]; int start = 0, end = 0; if (writeBuffer.hasArray()) { array = writeBuffer.array(); start = 0; end = writeBuffer.limit(); } System.out.println("write array:"); //zsw printHexValue(array, start, end); // loop on the channel.write() call since it will not necessarily // write all bytes in one shot try { while (nbytes != toWrite) { nbytes += channel.write(writeBuffer); try { Thread.sleep(Globals.CHANNEL_WRITE_SLEEP); } catch (InterruptedException e) { } } } catch (ClosedChannelException cce) { } catch (Exception e) { } // get ready for another write if needed writeBuffer.rewind(); } /** * write a String to a ByteBuffer, * prepended with a short integer representing the length of the String */ public static void putStr(ByteBuffer buff, String str) { if (str == null) { buff.putShort((short)0); } else { try { byte[] strData = str.getBytes("UTF-8"); buff.putShort((short)strData.length); buff.put(strData); } catch (UnsupportedEncodingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } } /** * read a String from a ByteBuffer * that was written w/the putStr method */ public static String getStr(ByteBuffer buff) { short len = buff.getShort(); if (len == 0) { return null; } else { byte[] b = new byte[len]; buff.get(b); String result = null; try { result = new String(b,"UTF-8" ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return result; } } /**16进制,打印byte数组*/ public static void printHexValue(byte[] data, int start, int end) { int value = 0; String hexStr = ""; String result = ""; result += "\""; for (int i = start; i < end; i++) { value = data[i] & 0xff; hexStr = Integer.toHexString(value); if (hexStr.length() < 2) { hexStr = "0" + hexStr; } result += hexStr + " "; } result += "\""; System.out.println(result); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?