波多在线播放_激情深爱五月_久久欧美精品_成人超碰_国产久_www.pixiv.moe


曙海教育集團論壇開發(fā)語言培訓(xùn)專區(qū)JAVA語言開發(fā) → 一個基礎(chǔ)的代理服務(wù)器類_Java語言基礎(chǔ)_Java基礎(chǔ)_Java開發(fā)_軟件開發(fā)


  共有7213人關(guān)注過本帖樹形打印

主題:一個基礎(chǔ)的代理服務(wù)器類_Java語言基礎(chǔ)_Java基礎(chǔ)_Java開發(fā)_軟件開發(fā)

美女呀,離線,留言給我吧!
wangxinxin
  1樓 個性首頁 | 博客 | 信息 | 搜索 | 郵箱 | 主頁 | UC


加好友 發(fā)短信
等級:青蜂俠 帖子:1393 積分:14038 威望:0 精華:0 注冊:2010-11-12 11:08:23
一個基礎(chǔ)的代理服務(wù)器類_Java語言基礎(chǔ)_Java基礎(chǔ)_Java開發(fā)_軟件開發(fā)  發(fā)帖心情 Post By:2010-12-11 9:40:35

/*************************************
   * 一個的代理類
   *************************************
   */
  import .*;
  import java.io.*;

  public class HttpProxy extends Thread {
    static public int CONNECT_RETRIES=5;
    static public int CONNECT_PAUSE=5;
    static public int TIMEOUT=50;
    static public int BUFSIZ=1024;
    static public boolean logging = false;
    static public OutputStream log=null;
    // 傳入數(shù)據(jù)用的
    protected Socket socket;
    // 上級代理服務(wù)器,可選
    static private String parent=null;
    static private int parentPort=-1;
    static public void setParentProxy(String name, int pport) {
   parent=name;
   parentPort=pport;
    }

    // 在給定Socket上創(chuàng)建一個代理。
    public HttpProxy(Socket s) { socket=s; start(); }

    public void writeLog(int c, boolean browser) throws IOException {
   log.write(c);
    }

    public void writeLog(byte[] bytes,int offset, int len, boolean browser) throws IOException {
   for (int i=0;i<len;i++) writeLog((int)bytes[offset+i],browser);
    }

    // 默認(rèn)情況下,日志信息輸出到
    // 標(biāo)準(zhǔn)輸出設(shè)備
    // 派生類可以覆蓋它
    public String processHostName(String url, String host, int port, Socket sock) {
  java.text.DateFormat cal=java.text.DateFormat.getDateTimeInstance();
  System.out.println(cal.format(new java.util.Date()) + " - " + url + " "
              + sock.getInetAddress()+"\n");
   return host;
    }

    // 執(zhí)行操作的線程



以字符為導(dǎo)向的stream基本上對有與之相對應(yīng)的以字節(jié)為導(dǎo)向的stream。兩個對應(yīng)類實現(xiàn)的功能相同,字是在操作時的導(dǎo)向不同。如CharArrayReader:和ByteArrayInputStream的作用都是把內(nèi)存中的一個緩沖區(qū)作為InputStream使用,所不同的是前者每次從內(nèi)存中讀取一個字節(jié)的信息,而后者每次從內(nèi)存中讀取一個字符。
1.3 兩種不現(xiàn)導(dǎo)向的stream之間的轉(zhuǎn)換
InputStreamReader和OutputStreamReader:把一個以字節(jié)為導(dǎo)向的stream轉(zhuǎn)換成一個以字符為導(dǎo)向的stream。
2. stream添加屬性
2.1 “為stream添加屬性”的作用
運用上面介紹的中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以為stream添加屬性。下面以一個例子來說明這種功能的作用。
如果我們要往一個文件中寫入數(shù)據(jù),我們可以這樣操作:
FileOutStream fs = new FileOutStream(“test.txt”);
然后就可以通過產(chǎn)生的fs對象調(diào)用write()函數(shù)來往test.txt文件中寫入數(shù)據(jù)了。但是,如果我們想實現(xiàn)“先把要寫入文件的數(shù)據(jù)先緩存到內(nèi)存中,再把緩存中的數(shù)據(jù)寫入文件中”的功能時,上面的API就沒有一個能滿足我們的需求了。但是通過FilterInputStream和FilterOutStream的子類,為FileOutStream添加我們所需要的功能。
2.2 FilterInputStream的各種類型
2.2.1 用于封裝以字節(jié)為導(dǎo)向的InputStream
1) DataInputStream:從stream中讀取基本類型(int、char等)數(shù)據(jù)。
2) BufferedInputStream:使用緩沖區(qū)
3) LineNumberInputStream:會記錄input stream內(nèi)的行數(shù),然后可以調(diào)用getLineNumber()和setLineNumber(int)
4) PushbackInputStream:很少用到,一般用于編譯器開發(fā)
2.2.2 用于封裝以字符為導(dǎo)向的InputStream
1) 沒有與DataInputStream對應(yīng)的類。除非在要使用readLine()時改用BufferedReader,否則使用DataInputStream
2) BufferedReader:與BufferedInputStream對應(yīng)
3) LineNumberReader:與LineNumberInputStream對應(yīng)
4) PushBackReader:與PushbackInputStream對應(yīng)
2.3 FilterOutStream的各種類型
2.2.3 用于封裝以字節(jié)為導(dǎo)向的OutputStream
1) DataIOutStream:往stream中輸出基本類型(int、char等)數(shù)據(jù)。
2) BufferedOutStream:使用緩沖區(qū)
3) PrintStream:產(chǎn)生格式化輸出
2.2.4 用于封裝以字符為導(dǎo)向的OutputStream
1) BufferedWrite:與對應(yīng)
2) PrintWrite:與對應(yīng)
3. RandomFile
1) 可通過RandomAccessFile對象完成對文件的讀寫操作
2) 在產(chǎn)生一個對象時,可指明要打開的文件的性質(zhì):r,只讀;w,只寫;rw可讀寫
3) 可以直接跳到文件中指定的位置
4. I/O應(yīng)用的一個例子




  





import .io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1.以行為單位從一個文件讀取數(shù)據(jù)
BufferedReader in =
new BufferedReader(
new FileReader("F:\\nepalon\\TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
in.close();

//1b. 接收鍵盤的輸入
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());

//2. 從一個String對象中讀取數(shù)據(jù)
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();

//3. 從內(nèi)存取出格式化輸入
try{
DataInputStream in3 =
new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.println((char)in3.readByte());
}
catch(EOFException e){
System.out.println("End of stream");
}

//4. 輸出到文件
try{
BufferedReader in4 =
new BufferedReader(
new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(
new FileWriter("F:\\nepalon\\ TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
}
atch(EOFException ex){
System.out.println("End of stream");
}

//5. 數(shù)據(jù)的存儲和恢復(fù)
try{
DataOutputStream out2 =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("F:\\nepalon\\ Data.txt")));
out2.writeDouble(3.1415926);
out2.writeChars("\nThas was pi:writeChars\n");
out2.writeBytes("Thas was pi:writeByte\n");
out2.close();
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("F:\\nepalon\\ Data.txt")));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
System.out.println(in5.readDouble());
System.out.println(in5br.readLine());
System.out.println(in5br.readLine());
}




  






catch(EOFException e){
System.out.println("End of stream");
}

//6. 通過RandomFile操作文件
RandomAccessFile rf =
new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
for(int i=0; i<10; i++)
rf.writeDouble(i*1.414);
rf.close();

rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();

rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();

rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();
}
}
關(guān)于代碼的解釋(以區(qū)為單位):
1區(qū)中,當(dāng)讀取文件時,先把文件內(nèi)容讀到緩存中,當(dāng)調(diào)用in.readLine()時,再從緩存中以字符的方式讀取數(shù)據(jù)(以下簡稱“緩存字節(jié)讀取方式”)。
1b區(qū)中,由于想以緩存字節(jié)讀取方式從標(biāo)準(zhǔn)IO(鍵盤)中讀取數(shù)據(jù),所以要先把標(biāo)準(zhǔn)IO(System.in)轉(zhuǎn)換成字符導(dǎo)向的stream,再進(jìn)行BufferedReader封裝。
2區(qū)中,要以字符的形式從一個String對象中讀取數(shù)據(jù),所以要產(chǎn)生一個StringReader類型的stream。
4區(qū)中,對String對象s2讀取數(shù)據(jù)時,先把對象中的數(shù)據(jù)存入緩存中,再從緩沖中進(jìn)行讀取;對TestIO.out文件進(jìn)行操作時,先把格式化后的信息輸出到緩存中,再把緩存中的信息輸出到文件中。
5區(qū)中,對Data.txt文件進(jìn)行輸出時,是先把基本類型的數(shù)據(jù)輸出屋緩存中,再把緩存中的數(shù)據(jù)輸出到文件中;對文件進(jìn)行讀取操作時,先把文件中的數(shù)據(jù)讀取到緩存中,再從緩存中以基本類型的形式進(jìn)行讀取。注意in5.readDouble()這一行。因為寫入第一個writeDouble(),所以為了正確顯示。也要以基本類型的形式進(jìn)行讀取。
6區(qū)是通過RandomAccessFile類對文件進(jìn)行操作。




  





    public void run() {
  String line;
  String host;
  int port=80;
        outbound=null;
   try {
     socket.setSoTimeout(TIMEOUT);
     InputStream is=socket.getInputStream();
     OutputStream os=null;
     try {
                // 獲取請求行的內(nèi)容
    line="";
    host="";
    int state=0;
    boolean space;
    while (true) {
      int c=is.read();
      if (c==-1) break;
      if (logging) writeLog(c,true);
      space=Character.isWhitespace((char)c);
      switch (state) {
      case 0:
     if (space) continue;
          state=1;
      case 1:
     if (space) {
       state=2;
       continue;
     }
     line=line+(char)c;
     break;
      case 2:
     if (space) continue; // 跳過多個空白字符
            state=3;
      case 3:
     if (space) {
       state=4;

支持(0中立(0反對(0單帖管理 | 引用 | 回復(fù) 回到頂部

返回版面帖子列表

一個基礎(chǔ)的代理服務(wù)器類_Java語言基礎(chǔ)_Java基礎(chǔ)_Java開發(fā)_軟件開發(fā)








簽名
主站蜘蛛池模板: 久爱视频www在线播放 | 亚洲高清在线视频 | 泰国一级毛片aaa下面毛多 | 国产精品久久久久久久久久久久久 | 婷婷国产在线观看 | 日本一区二区三区免费观看 | 亚洲精品在 | 日韩国产第一页 | 国产成人综合一区二区三区 | 国产美女福利视频福利 | 亚洲精品视频一区 | 欧美 日本 国产 | 欧美啊啊啊 | 国内精品视频区在线2021 | 色婷婷成人做爰A片免费看网站 | 天天干天天色 | 六月丁香综合 | 精品国产不卡一区二区三区 | 日本黄视频在线观看 | 亚洲一区二区三区中文字幕 | 久草福利资源网站免费 | 国产精品密蕾丝视频下载 | 国产11一12周岁女毛片 | 二区国产 | 欧美日韩综合在线视频免费看 | 国产成人一区二区三区久久久 | 国产一区二区久久 | 狠狠久久 | 欧美激情精品久久久久 | 在线免费观看h片 | 国产午夜免费视频片夜色 | 久久免费看少妇高潮A片JA小说 | 久久国产视频一区 | 亚洲精品三级 | 三级黄色片在线观看 | 欧美成人影院在线观看三级 | 色婷婷一区二区三区四区 | 久久久久成人精品免费播放 | 欧日韩不卡在线视频 | 污污视频免费网站 | 亚洲午夜精品A片久久不卡蜜桃 |