2010-06-29 135 views

回答

0

由於retrieveFilestoreFile處理輸入和輸出流,有可能是你寫你自己的子類,它可以在或出過傳輸的字節數一定時間?

1

試試這個:

public class ReportingOutputStream extends OutputStream { 
    public static final String BYTES_PROP = "Bytes"; 
    private FileOutputStream fileStream; 
    private long byteCount = 0L; 
    private long lastByteCount = 0L; 
    private long updateInterval = 1L << 10; 
    private long nextReport = updateInterval; 
    private PropertyChangeSupport changer = new PropertyChangeSupport(this); 

    public ReportingOutputStream(File f) throws IOException { 
     fileStream = new FileOutputStream(f); 
    } 

    public void setUpdateInterval(long bytes) { 
     updateInterval = bytes; 
     nextReport = updateInterval; 
    } 

    @Override 
    public void write(int b) throws IOException { 
     byte[] bytes = { (byte) (b & 0xFF) }; 
     write(bytes, 0, 1); 
    } 

    @Override 
    public void write(byte[] b, int off, int len) throws IOException { 
     fileStream.write(b, off, len); 
     byteCount += len; 
     if (byteCount > nextReport) { 
      changer.firePropertyChange(BYTES_PROP, lastByteCount, byteCount); 
      lastByteCount = byteCount; 
      nextReport += updateInterval; 
     } 
    } 

    @Override 
    public void close() throws IOException { 
     if (fileStream != null) { 
      fileStream.close(); 
      fileStream = null; 
     } 
    } 

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { 
     changer.removePropertyChangeListener(propertyName, listener); 
    } 

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { 
     changer.addPropertyChangeListener(propertyName, listener); 
    } 
} 

創建流後,添加屬性更改偵聽BYTES_PROP。默認情況下,它會觸發每接收到1 KB的處理程序。致電setUpdateInterval更改。