2012-02-14 4 views
4

나는 BufferedOutputStream의 이론을 이해합니다. 바이트가 꽉 찰 때까지 버퍼 배열에 기록 된 다음 기본 스트림에 기록 (플러시)됩니다. 즉, OS 호출이 적을수록 바이트 단위로 기록하는 것이 더 빠릅니다.BufferedOutputStream은 실제로 낮은 수준에서 어떻게 작동합니까?

그러나, BufferedOutputStream 클래스 및 메서드 (BufferedOutputStream.java)의 구현을 살펴보면 궁극적으로 버퍼의 바이트가 바이트 단위로 기록됩니다.

난이 경우 생각 때문에 : BufferedOutputStream.write에서

(바이트 B [] 오프 INT, INT LEN)가있는 라인 out.write (b 오프 렌). out는 OutputStream의 인스턴스이기 (위해) 때문에, BufferedOutputStream는 아니기 때문에 OutputStream.write (byte [], int, int)를 호출하고 있습니다. 이것은 for 루프를 사용하여 바이트 단위로 작성합니다.

실제로 진행되고있는 작업과 그 작업이 더 빠른 방법을 명확히 할 수 있습니까? 귀하의 링크에서

+1

버퍼'out.write (바이트 오프셋, 길이)를 사용하여 플러싱'어떻게 그 바이트 단위? 더 자세하게 얘기해 주 시겠어요? –

+1

그러나 Out.write (bytes, offset, length)는 OutputStream.java에서 write (byte b [], int off, int len) 메서드를 호출하지 않습니까? 그런 다음 for 루프를 사용하여 버퍼의 각 바이트를 개별적으로 씁니다. – user1209776

+3

'OutputStream'은 추상적입니다. 대부분의 서브 클래스는이 메소드를보다 효율적인 버젼으로 오버라이드 (override)합니다. 예를 들어 [FileOutputStream] (http://www.docjar.com/html/api/java/io/FileOutputStream.java.html)을보십시오. –

답변

1

:

/** Flush the internal buffer */ 
    private void flushBuffer() throws IOException { 
     if (count > 0) { 
      out.write(buf, 0, count); 
      count = 0; 
     } 
    } 

...

/** 
    * Flushes this buffered output stream. This forces any buffered 
    * output bytes to be written out to the underlying output stream. 
    * 
    * @exception IOException if an I/O error occurs. 
    * @see  java.io.FilterOutputStream#out 
    */ 
    public synchronized void flush() throws IOException { 
     flushBuffer(); 
     out.flush(); 
    } 

당신이 볼 수 있듯이, flush()는 모든 버퍼 하나 가 기본이되는 스트림에 갈 내용과 계단식를 기록 홍조. BufferedOutputStream 그런 다음 버퍼에 쓸 때마다 write(byte b[], int off, int len)void write(int b) (모든 쓰기가 위임되는 클래스의 핵심 메서드)을 다시 구현하고 필요한 경우 플러시합니다.

0

코드 상태 :이 모든 현재 버퍼 바이트의 쓰기입니다

79  /** Flush the internal buffer */ 
80  private void flushBuffer() throws IOException { 
81   if (count > 0) { 
82    out.write(buf, 0, count); 
83    count = 0; 
84   } 
85  } 

. 바이트 단위가 아닙니다.

+0

하지만 바이트 단위로 작업을 작성 (buf, 0, count)하지 않습니까? ... (int i = 0; i user1209776

0

사용자BufferedOutputStream 인 것은 실제로 모든 바이트가 전송 될 때까지 기다릴 필요가 없다는 아이디어입니다. 사용자는 연결 자체가 느린 경우에도 더 큰 블록을 출력 스트림으로 밀어 넣고 계속할 수 있습니다. 따라서이 쪽이 더 빠릅니다. outputstream 자체는 최대한 빨리 시도합니다.

1

데이터가 플러시되면 블록으로 표시됩니다.

79  /** Flush the internal buffer */ 
80  private void flushBuffer() throws IOException { 
81   if (count > 0) { 
82    out.write(buf, 0, count); 
83    count = 0; 
84   } 
85  } 

FileOutputStream에 다른 많은 재정 OutputStream.write()는 효율적으로 데이터 블록을 처리합니다.

http://www.docjar.com/html/api/java/io/FileOutputStream.java.html

284 
285  /** 
286  * Writes a sub array as a sequence of bytes. 
287  * @param b the data to be written 
288  * @param off the start offset in the data 
289  * @param len the number of bytes that are written 
290  * @param append {@code true} to first advance the position to the 
291  *  end of file 
292  * @exception IOException If an I/O error has occurred. 
293  */ 
294  private native void writeBytes(byte b[], int off, int len, boolean append) 
295   throws IOException; 

308  /** 
309  * Writes <code>len</code> bytes from the specified byte array 
310  * starting at offset <code>off</code> to this file output stream. 
311  * 
312  * @param  b  the data. 
313  * @param  off the start offset in the data. 
314  * @param  len the number of bytes to write. 
315  * @exception IOException if an I/O error occurs. 
316  */ 
317  public void write(byte b[], int off, int len) throws IOException { 
318   writeBytes(b, off, len, append); 
319  } 
+0

(위의 설명 참조) – user1209776

+0

write (buffer, start, len)가 무시되지 않으면 버퍼가 무의미합니다. 대부분의 스트림이이를 오버라이드합니다. –

+0

BufferedOutputStream.write (byte b [], int off, int len)에는 out.write (b, off, len)를 씁니다. 이것이 어떤 글을 가리키는가? – user1209776

관련 문제