`
yj10864
  • 浏览: 82730 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
1 1
We can't reproduce the security issue that JsessionID requests can succeed after modification.
 Can you tell me your steps?
 We need to know how did you modify the JsessionID.


According our tests,if characters are appended to JessionID, the modification will be invalid,but this way,it will not take any effect.
排序 java 实现二分查找法
/**
 * 二分查找又称折半查找,它是一种效率较高的查找方法。 
  【二分查找要求】:1.必须采用顺序存储结构 2.必须按关键字大小有序排列。
 * @author Administrator
 *
 */
public class BinarySearch { 
	public static void main(String[] args) {
		int[] src = new int[] {1, 3, 5, 7, 8, 9}; 
		System.out.println(binarySearch(src, 3));
		System.out.println(binarySearch(src,3,0,src.length-1));
	}

	/**
	 * * 二分查找算法 * *
	 * 
	 * @param srcArray
	 *            有序数组 *
	 * @param des
	 *            查找元素 *
	 * @return des的数组下标,没找到返回-1
	 */ 
   public static int binarySearch(int[] srcArray, int des){ 
	
		int low = 0; 
		int high = srcArray.length-1; 
		while(low <= high) { 
			int middle = (low + high)/2; 
			if(des == srcArray[middle]) { 
			    return middle; 
			}else if(des <srcArray[middle]) { 
			    high = middle - 1; 
			}else { 
			    low = middle + 1; 
			}
		}
		return -1;
   }
      
	  /**  
	 *二分查找特定整数在整型数组中的位置(递归)  
	 *@paramdataset  
	 *@paramdata  
	 *@parambeginIndex  
	 *@paramendIndex  
	 *@returnindex  
	 */
	public static int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){  
	   int midIndex = (beginIndex+endIndex)/2;  
	   if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){
		   return -1;  
	   }
	   if(data <dataset[midIndex]){  
		   return binarySearch(dataset,data,beginIndex,midIndex-1);  
	   }else if(data>dataset[midIndex]){  
	       return binarySearch(dataset,data,midIndex+1,endIndex);  
	   }else {  
		   return midIndex;  
	   }  
   } 

}
冒泡排序 java冒泡排序
public class BubbleSort
{
    
    
    public static void main(String[] args){
        int[] values ={
                3,1,6,2,9,0,7,4,5
        };
        
        sort(values);
        for(int i=0; i < values.length; ++i){
            
            System.out.println("Index: " + i + "Value: " + values[i]);
            
        }
        
        
    }
    
    
    public static void sort(int[]  values){
        
        
        int temp;
        
        for(int i=0 ; i < values.length ; ++i){
            
            for(int j=0; j <values.length - i - 1; ++j){
                
                if(values[j] > values[j + 1]){
                    temp = values[j];
                    values[j] = values[j + 1];
                    values[j + 1] = temp;
                    
                    
                    
                }
                
                
                
            }
            
            
        }
    }

}
文件 File(文件类)
package com.test.file;

import java.io.File;
/**
 * File提供程序与外部设备目录和文件的管理(磁盘)
 * 可以获取目录和文件相关信息,也可以修改文件和目录的信息
 * 提供判断、修改、创建文件或者目录的相关操作
 * @author 守望幸福
 *
 */
public class FileTest {
	private static void print(String str){
		System.out.println(str);
	}
	public static void main(String[] args) {
		File f=new File("d:\\fileTest\\file.txt");//绝对路径创建File对象
		print("是否可读:"+f.canRead());
		print("是否可写:"+f.canWrite());
		print("是否存在:"+f.exists());
		print("绝对路径名:"+f.getAbsolutePath());
		print("名称:"+f.getName());
		print("父目录:"+f.getParent());
		print("路径:"+f.getPath());
		print("是否绝对路径:"+f.isAbsolute());
		print("是否为目录:"+f.isDirectory());
		print("是否为文件:"+f.isFile());
		print("是否隐藏:"+f.isHidden());
		print("长度:"+f.length());
		print("最后修改时间:"+f.lastModified());
		print("目录与文件名:"+f.list());
		print("URI:"+f.toURI().toString());
	}
	
}
排序 Java排序汇总
import java.util.Random;
 
/**
 * 排序测试类
 *
 * 排序算法的分类如下:
 * 1.插入排序(直接插入排序、折半插入排序、希尔排序);
 * 2.交换排序(冒泡泡排序、快速排序);
 * 3.选择排序(直接选择排序、堆排序);
 * 4.归并排序;
 * 5.基数排序。
 *
 * 关于排序方法的选择:
 * (1)若n较小(如n≤50),可采用直接插入或直接选择排序。
 *  当记录规模较小时,直接插入排序较好;否则因为直接选择移动的记录数少于直接插人,应选直接选择排序为宜。
 * (2)若文件初始状态基本有序(指正序),则应选用直接插人、冒泡或随机的快速排序为宜;
 * (3)若n较大,则应采用时间复杂度为O(nlgn)的排序方法:快速排序、堆排序或归并排序。
 *
 */
public class SortTest {
 
       /**
        * 初始化测试数组的方法
        * @return 一个初始化好的数组
        */
       public int[] createArray() {
              Random random = new Random();
              int[] array = new int[10];
              for (int i = 0; i < 10; i++) {
                     array[i] = random.nextInt(100) - random.nextInt(100);//生成两个随机数相减,保证生成的数中有负数
              }
              System.out.println("==========原始序列==========");
              printArray(array);
              return array;
       }
 
       /**
        * 打印数组中的元素到控制台
        * @param source
        */
       public void printArray(int[] data) {
              for (int i : data) {
                     System.out.print(i + " ");
              }
              System.out.println();
       }
 
       /**
        * 交换数组中指定的两元素的位置
        * @param data
        * @param x
        * @param y
        */
       private void swap(int[] data, int x, int y) {
              int temp = data[x];
              data[x] = data[y];
              data[y] = temp;
       }
 
       /**
        * 冒泡排序----交换排序的一种
        * 方法:相邻两元素进行比较,如有需要则进行交换,每完成一次循环就将最大元素排在最后(如从小到大排序),下一次循环是将其他的数进行类似操作。
        * 性能:比较次数O(n^2),n^2/2;交换次数O(n^2),n^2/4
        *
        * @param data 要排序的数组
        * @param sortType 排序类型
        * @return
        */
       public void bubbleSort(int[] data, String sortType) {
              if (sortType.equals("asc")) { //正排序,从小排到大
                     //比较的轮数
                     for (int i = 1; i < data.length; i++) {
                            //将相邻两个数进行比较,较大的数往后冒泡
                            for (int j = 0; j < data.length - i; j++) {
                                   if (data[j] > data[j + 1]) {
                                          //交换相邻两个数
                                          swap(data, j, j + 1);
                                   }
                            }
                     }
              } else if (sortType.equals("desc")) { //倒排序,从大排到小
                     //比较的轮数
                     for (int i = 1; i < data.length; i++) {
                            //将相邻两个数进行比较,较大的数往后冒泡
                            for (int j = 0; j < data.length - i; j++) {
                                   if (data[j] < data[j + 1]) {
                                          //交换相邻两个数
                                          swap(data, j, j + 1);
                                   }
                            }
                     }
              } else {
                     System.out.println("您输入的排序类型错误!");
              }
              printArray(data);//输出冒泡排序后的数组值
       }
 
       /**
        * 直接选择排序法----选择排序的一种
        * 方法:每一趟从待排序的数据元素中选出最小(或最大)的一个元素, 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
        * 性能:比较次数O(n^2),n^2/2
        *       交换次数O(n),n
        *       交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CUP时间多,所以选择排序比冒泡排序快。
        *       但是N比较大时,比较所需的CPU时间占主要地位,所以这时的性能和冒泡排序差不太多,但毫无疑问肯定要快些。
        *
        * @param data 要排序的数组
        * @param sortType 排序类型
        * @return
        */
       public void selectSort(int[] data, String sortType) {
 
              if (sortType.equals("asc")) { //正排序,从小排到大
                     int index;
                     for (int i = 1; i < data.length; i++) {
                            index = 0;
                            for (int j = 1; j <= data.length - i; j++) {
                                   if (data[j] > data[index]) {
                                          index = j;
                                   }
                            }
                            //交换在位置data.length-i和index(最大值)两个数
                            swap(data, data.length - i, index);
                     }
              } else if (sortType.equals("desc")) { //倒排序,从大排到小
                     int index;
                     for (int i = 1; i < data.length; i++) {
                            index = 0;
                            for (int j = 1; j <= data.length - i; j++) {
                                   if (data[j] < data[index]) {
                                          index = j;
 
                                   }
                            }
                            //交换在位置data.length-i和index(最大值)两个数
                            swap(data, data.length - i, index);
                     }
              } else {
                     System.out.println("您输入的排序类型错误!");
              }
              printArray(data);//输出直接选择排序后的数组值
       }
 
       /**
        * 插入排序
        * 方法:将一个记录插入到已排好序的有序表(有可能是空表)中,从而得到一个新的记录数增1的有序表。
        * 性能:比较次数O(n^2),n^2/4
        *       复制次数O(n),n^2/4
        *       比较次数是前两者的一般,而复制所需的CPU时间较交换少,所以性能上比冒泡排序提高一倍多,而比选择排序也要快。
        *
        * @param data 要排序的数组
        * @param sortType 排序类型
        */
       public void insertSort(int[] data, String sortType) {
              if (sortType.equals("asc")) { //正排序,从小排到大
                     //比较的轮数
                     for (int i = 1; i < data.length; i++) {
                            //保证前i+1个数排好序
                            for (int j = 0; j < i; j++) {
                                   if (data[j] > data[i]) {
                                          //交换在位置j和i两个数
                                          swap(data, i, j);
                                   }
                            }
                     }
              } else if (sortType.equals("desc")) { //倒排序,从大排到小
                     //比较的轮数
                     for (int i = 1; i < data.length; i++) {
                            //保证前i+1个数排好序
                            for (int j = 0; j < i; j++) {
                                   if (data[j] < data[i]) {
                                          //交换在位置j和i两个数
                                          swap(data, i, j);
                                   }
                            }
                     }
              } else {
                     System.out.println("您输入的排序类型错误!");
              }
              printArray(data);//输出插入排序后的数组值
       }
 
       /**
        * 反转数组的方法
        * @param data 源数组
        */
       public void reverse(int[] data) {
 
              int length = data.length;
              int temp = 0;//临时变量
 
              for (int i = 0; i < length / 2; i++) {
                     temp = data[i];
                     data[i] = data[length - 1 - i];
                     data[length - 1 - i] = temp;
              }
              printArray(data);//输出到转后数组的值
       }
 
       /**
        * 快速排序
        * 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。
        * 步骤为:
        * 1. 从数列中挑出一个元素,称为 "基准"(pivot),
        * 2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分割之后,该基准是它的最后位置。这个称为分割(partition)操作。
        * 3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。
        * 递回的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了。虽然一直递回下去,但是这个算法总会结束,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。
        * @param data 待排序的数组
        * @param low
        * @param high
        * @see SortTest#qsort(int[], int, int)
        * @see SortTest#qsort_desc(int[], int, int)
        */
       public void quickSort(int[] data, String sortType) {
              if (sortType.equals("asc")) { //正排序,从小排到大
                     qsort_asc(data, 0, data.length - 1);
              } else if (sortType.equals("desc")) { //倒排序,从大排到小
                     qsort_desc(data, 0, data.length - 1);
              } else {
                     System.out.println("您输入的排序类型错误!");
              }
       }
 
       /**
        * 快速排序的具体实现,排正序
        * @param data
        * @param low
        * @param high
        */
       private void qsort_asc(int data[], int low, int high) {
              int i, j, x;
              if (low < high) { //这个条件用来结束递归
                     i = low;
                     j = high;
                     x = data[i];//用x变量先保存这个值,以便比较和最后的存放
                                         //这里实际上是选择了第一个数作为基准点
                     while (i < j) {
                            while (i < j && data[j] > x) {
                                   j--; //从右向左找第一个小于x的数
                            }
                            if (i < j) {//找到了小于x的数
                                   data[i] = data[j];
                                   i++;
                            }
                            while (i < j && data[i] < x) {
                                   i++; //从左向右找第一个大于x的数
                            }
                            if (i < j) {
                                   data[j] = data[i];
                                   j--;
                            }
                     }
                     data[i] = x;//此时i和j相遇(相等)了
                     qsort_asc(data, low, i - 1);
                     qsort_asc(data, i + 1, high);
              }
       }
 
       /**
        * 快速排序的具体实现,排倒序
        * @param data
        * @param low
        * @param high
        */
       private void qsort_desc(int data[], int low, int high) {
              int i, j, x;
              if (low < high) { //这个条件用来结束递归
                     i = low;
                     j = high;
                     x = data[i];
                     while (i < j) {
                            while (i < j && data[j] < x) {
                                   j--; //从右向左找第一个小于x的数
                            }
                            if (i < j) {
                                   data[i] = data[j];
                                   i++;
                            }
                            while (i < j && data[i] > x) {
                                   i++; //从左向右找第一个大于x的数
                            }
                            if (i < j) {
                                   data[j] = data[i];
                                   j--;
                            }
                     }
                     data[i] = x;
                     qsort_desc(data, low, i - 1);
                     qsort_desc(data, i + 1, high);
              }
       }
 
       /**
        *二分查找特定整数在整型数组中的位置(递归)
        *查找线性表必须是有序列表
        *@paramdataset
        *@paramdata
        *@parambeginIndex
        *@paramendIndex
        *@returnindex
        */
       public int binarySearch(int[] dataset, int data, int beginIndex,
                     int endIndex) {
              int midIndex = (beginIndex + endIndex) >>> 1; //相当于mid = (low + high) / 2,但是效率会高些
              if (data < dataset[beginIndex] || data > dataset[endIndex]
                            || beginIndex > endIndex)
                     return -1;
              if (data < dataset[midIndex]) {
                     return binarySearch(dataset, data, beginIndex, midIndex - 1);
              } else if (data > dataset[midIndex]) {
                     return binarySearch(dataset, data, midIndex + 1, endIndex);
              } else {
                     return midIndex;
              }
       }
 
       /**
        *二分查找特定整数在整型数组中的位置(非递归)
        *查找线性表必须是有序列表
        *@paramdataset
        *@paramdata
        *@returnindex
        */
       public int binarySearch(int[] dataset, int data) {
              int beginIndex = 0;
              int endIndex = dataset.length - 1;
              int midIndex = -1;
              if (data < dataset[beginIndex] || data > dataset[endIndex]
                            || beginIndex > endIndex)
                     return -1;
              while (beginIndex <= endIndex) {
                     midIndex = (beginIndex + endIndex) >>> 1; //相当于midIndex = (beginIndex + endIndex) / 2,但是效率会高些
                     if (data < dataset[midIndex]) {
                            endIndex = midIndex - 1;
                     } else if (data > dataset[midIndex]) {
                            beginIndex = midIndex + 1;
                     } else {
                            return midIndex;
                     }
              }
              return -1;
       }
 
       public static void main(String[] args) {
              SortTest sortTest = new SortTest();
 
              int[] array = sortTest.createArray();
 
              System.out.println("==========冒泡排序后(正序)==========");
              sortTest.bubbleSort(array, "asc");
              System.out.println("==========冒泡排序后(倒序)==========");
              sortTest.bubbleSort(array, "desc");
 
              array = sortTest.createArray();
 
              System.out.println("==========倒转数组后==========");
              sortTest.reverse(array);
 
              array = sortTest.createArray();
 
              System.out.println("==========选择排序后(正序)==========");
              sortTest.selectSort(array, "asc");
              System.out.println("==========选择排序后(倒序)==========");
              sortTest.selectSort(array, "desc");
 
              array = sortTest.createArray();
 
              System.out.println("==========插入排序后(正序)==========");
              sortTest.insertSort(array, "asc");
              System.out.println("==========插入排序后(倒序)==========");
              sortTest.insertSort(array, "desc");
 
              array = sortTest.createArray();
              System.out.println("==========快速排序后(正序)==========");
              sortTest.quickSort(array, "asc");
              sortTest.printArray(array);
              System.out.println("==========快速排序后(倒序)==========");
              sortTest.quickSort(array, "desc");
              sortTest.printArray(array);
 
              System.out.println("==========数组二分查找==========");
              System.out.println("您要找的数在第" + sortTest.binarySearch(array, 74)
                            + "个位子。(下标从0计算)");
       }
}
java zip压缩 zip压缩 Java ZIP 压缩
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestZip
{
    
    public TestZip()
    {
        
    }
    
    public static void main(String[] args) throws IOException
    {
        new TestZip().zip("D:\\temp\\testTerm\\sourceFile", "D:\\temp\\testTerm\\myRtn.zip");
    }
    
    private static final int BUFFEREDSIZE = 1024; 
    
    public synchronized void zip(String inputFilename, String zipFilename) throws IOException {    
        zip(new File(inputFilename), zipFilename);    
    }    
        
    public synchronized void zip(File inputFile, String zipFilename) throws IOException {    
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));    
   
        try {    
            zip(inputFile, out, "");    
        } catch (IOException e) {    
            throw e;    
        } finally {    
            out.close();    
        }    
    }    
   
    private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException {    
        if (inputFile.isDirectory()) { 
            File[] inputFiles = inputFile.listFiles();    
            out.putNextEntry(new ZipEntry(base + "/"));   
            base = base.length() == 0 ? "" : base + "/";    
            for (int i = 0; i < inputFiles.length; i++) {  
                zip(inputFiles[i], out, base + inputFiles[i].getName());    
            }    
        } else {    
            if (base.length() > 0) {    
                out.putNextEntry(new ZipEntry(base));    
            } else {    
                out.putNextEntry(new ZipEntry(inputFile.getName()));    
            }    
   
            FileInputStream in = new FileInputStream(inputFile);    
            try {    
                int c;    
                byte[] by = new byte[BUFFEREDSIZE];    
                while ((c = in.read(by)) != -1) {    
                    out.write(by, 0, c);    
                }    
            } catch (IOException e) {    
                throw e;    
            } finally {   
                in.close();    
            }    
        }    
    }
    
}
java动态编译 Java动态编译
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * 动态编译Java代码
 */
public class DynamicCompileTest {
	public static void main(String[] args) {
		File file = null;
		String classname = null;
		try {
			String classDir = System.getProperty("user.dir");
			file = File.createTempFile("RunTime", ".java", new File(classDir));
			String filename = file.getName();
			classname = filename.substring(0, filename.lastIndexOf('.'));
			PrintWriter out = new PrintWriter(new FileOutputStream(file));
			// 代码
			String code = "public class " + classname + " {"
					+ "public void run() {"
					+ "System.out.println(\"DynamicCompile Success.\");" + "}}";
			out.println(code);
			out.flush();
			out.close();
			// 编译
			String[] arg = new String[] { "-d", classDir, filename };
			com.sun.tools.javac.Main.compile(arg);// JAVA_HOME/jdk/lib/tools.jar
			URL url = new URL("file:/" + classDir + File.separator);
			// 动态加载/执行
			URLClassLoader loader = new URLClassLoader(new URL[] { url });
			Class<?> clazz = loader.loadClass(classname);
			Object obj = clazz.newInstance();
			Method method = clazz.getMethod("run");
			method.invoke(obj);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (file != null) {
				file.delete();
				file = new File(classname + ".class");
				if (file != null)
					file.delete();
			}
		}
	}
}
NIO Client nio 解读NIO Socket非阻塞模式

/**
 * 
 * @author Jeff
 *
 */
public class HelloWorldClient {

	static int SIZE = 10;
	static InetSocketAddress ip = new InetSocketAddress("localhost", 8888);
	static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();

	static class Message implements Runnable {
		protected String name;
		String msg = "";

		public Message(String index) {
			this.name = index;
		}

		public void run() {
			try {
				long start = System.currentTimeMillis();
				//打开Socket通道
				SocketChannel client = SocketChannel.open();
				//设置为非阻塞模式
				client.configureBlocking(false);
				//打开选择器
				Selector selector = Selector.open();
				//注册连接服务端socket动作
				client.register(selector, SelectionKey.OP_CONNECT);
				//连接
				client.connect(ip);
				//分配内存
				ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
				int total = 0;

				_FOR: for (;;) {
					selector.select();
					Iterator iter = selector.selectedKeys().iterator();

					while (iter.hasNext()) {
						SelectionKey key = (SelectionKey) iter.next();
						iter.remove();
						if (key.isConnectable()) {
							SocketChannel channel = (SocketChannel) key
									.channel();
							if (channel.isConnectionPending())
								channel.finishConnect();
							channel
									.write(encoder
											.encode(CharBuffer.wrap(name)));

							channel.register(selector, SelectionKey.OP_READ);
						} else if (key.isReadable()) {
							SocketChannel channel = (SocketChannel) key
									.channel();
							int count = channel.read(buffer);
							if (count > 0) {
								total += count;
								buffer.flip();

								while (buffer.remaining() > 0) {
									byte b = buffer.get();
									msg += (char) b;
									
								}

								buffer.clear();
							} else {
								client.close();
								break _FOR;
							}
						}
					}
				}
				double last = (System.currentTimeMillis() - start) * 1.0 / 1000;
				System.out.println(msg + "used time :" + last + "s.");
				msg = "";
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
	
		String names[] = new String[SIZE];

		for (int index = 0; index < SIZE; index++) {
			names[index] = "jeff[" + index + "]";
			new Thread(new Message(names[index])).start();
		}
	
	}
}
NIO Server nio 解读NIO Socket非阻塞模式
/**
 * 
 * @author Jeff
 *
 */
public class HelloWorldServer {

	static int BLOCK = 1024;
	static String name = "";
	protected Selector selector;
	protected ByteBuffer clientBuffer = ByteBuffer.allocate(BLOCK);
	protected CharsetDecoder decoder;
	static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();

	public HelloWorldServer(int port) throws IOException {
		selector = this.getSelector(port);
		Charset charset = Charset.forName("GB2312");
		decoder = charset.newDecoder();
	}

	// 获取Selector
	protected Selector getSelector(int port) throws IOException {
		ServerSocketChannel server = ServerSocketChannel.open();
		Selector sel = Selector.open();
		server.socket().bind(new InetSocketAddress(port));
		server.configureBlocking(false);
		server.register(sel, SelectionKey.OP_ACCEPT);
		return sel;
	}

	// 监听端口
	public void listen() {
		try {
			for (;;) {
				selector.select();
				Iterator iter = selector.selectedKeys().iterator();
				while (iter.hasNext()) {
					SelectionKey key = (SelectionKey) iter.next();
					iter.remove();
					process(key);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 处理事件
	protected void process(SelectionKey key) throws IOException {
		if (key.isAcceptable()) { // 接收请求
			ServerSocketChannel server = (ServerSocketChannel) key.channel();
			SocketChannel channel = server.accept();
			//设置非阻塞模式
			channel.configureBlocking(false);
			channel.register(selector, SelectionKey.OP_READ);
		} else if (key.isReadable()) { // 读信息
			SocketChannel channel = (SocketChannel) key.channel();
			int count = channel.read(clientBuffer);
			if (count > 0) {
				clientBuffer.flip();
				CharBuffer charBuffer = decoder.decode(clientBuffer);
				name = charBuffer.toString();
				// System.out.println(name);
				SelectionKey sKey = channel.register(selector,
						SelectionKey.OP_WRITE);
				sKey.attach(name);
			} else {
				channel.close();
			}

			clientBuffer.clear();
		} else if (key.isWritable()) { // 写事件
			SocketChannel channel = (SocketChannel) key.channel();
			String name = (String) key.attachment();
			
			ByteBuffer block = encoder.encode(CharBuffer
					.wrap("Hello !" + name));
			

			channel.write(block);

			//channel.close();

		}
	}

	public static void main(String[] args) {
		int port = 8888;
		try {
			HelloWorldServer server = new HelloWorldServer(port);
			System.out.println("listening on " + port);
			
			server.listen();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Global site tag (gtag.js) - Google Analytics