日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

记录一次Socket编程:OutputStream的flush方法

發(fā)布時(shí)間:2025/3/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记录一次Socket编程:OutputStream的flush方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

先上源碼:

/*** Flushes this output stream and forces any buffered output bytes* to be written out. The general contract of <code>flush</code> is* that calling it is an indication that, if any bytes previously* written have been buffered by the implementation of the output* stream, such bytes should immediately be written to their* intended destination.* <p>* If the intended destination of this stream is an abstraction provided by* the underlying operating system, for example a file, then flushing the* stream guarantees only that bytes previously written to the stream are* passed to the operating system for writing; it does not guarantee that* they are actually written to a physical device such as a disk drive.* <p>* The <code>flush</code> method of <code>OutputStream</code> does nothing.** @exception IOException if an I/O error occurs.*/public void flush() throws IOException {}

總結(jié)有以下幾點(diǎn):

1. flush()下達(dá)一條命令給緩沖區(qū),讓它將所儲(chǔ)存的數(shù)據(jù)全部清空,即發(fā)送給下一級(jí)

2. flush()刷空輸出流,并輸出所有被緩存的字節(jié)。由于某些流支持緩存功能,該方法將把緩存中所有內(nèi)容強(qiáng)制輸出到流中。

3.?OutputStream.flush()方法將所有寫(xiě)入到OutputStream的數(shù)據(jù)沖刷到相應(yīng)的目標(biāo)媒介中。比如,如果輸出流是FileOutputStream,那么寫(xiě)入到其中的數(shù)據(jù)可能并沒(méi)有真正寫(xiě)入到磁盤(pán)中。即使所有數(shù)據(jù)都寫(xiě)入到了FileOutputStream,這些數(shù)據(jù)還是有可能保留在內(nèi)存的緩沖區(qū)中。通過(guò)調(diào)用flush()方法,可以把緩沖區(qū)內(nèi)的數(shù)據(jù)刷新到磁盤(pán)(或者網(wǎng)絡(luò),以及其他任何形式的目標(biāo)媒介)中。

例子:

// 模擬瀏覽器,給tomcat服務(wù)端發(fā)送符合http協(xié)議的請(qǐng)求消息 public static void main(String[] args) throws IOException {Socket s = new Socket("127.0.0.1", 80);PrintWriter out = new PrintWriter(s.getOutputStream());out.println("GET /myweb/test.jsp HTTP/1.1");out.println("Accept: */*");out.println("Accept-Language: zh-CN");out.println("Accept-Encoding: gzip, deflate");out.println();out.flush(); // 清空緩存并輸出流InputStream in = s.getInputStream();byte b[] = new byte[1024];int leng = 0;while((leng = in .read(b)) != -1){String str = new String(b, 0, leng);System.out.println(str);}s.close(); }

?

總結(jié)

以上是生活随笔為你收集整理的记录一次Socket编程:OutputStream的flush方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。