home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / BufferStream.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  3.0 KB  |  145 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "BufferStream.h"
  20.  
  21. #include "client.h"
  22.  
  23. #ifdef PROFILE
  24. #pragma profile on
  25. #endif
  26.  
  27. #define STREAM_BUFFER_SIZE    32000
  28.  
  29. LFileBufferStream::LFileBufferStream( FSSpec& inFileSpec ): LFileStream( inFileSpec )
  30. {
  31.     fBuffer = NULL;
  32.     fBufferSize = 0;
  33.     fLastWritten = 0;
  34.     fUseBuffer = FALSE;
  35.     fURL = NULL;
  36.     fWriteFailed = FALSE;
  37. }
  38.  
  39. LFileBufferStream::~LFileBufferStream()
  40. {
  41.     Try_
  42.     {
  43.         FlushBuffer( FALSE );
  44.     }
  45.     Catch_(inErr)
  46.     {
  47.     }
  48.     EndCatch_
  49.     if ( fURL )
  50.         XP_FREE( fURL );
  51. }
  52.  
  53. OSErr LFileBufferStream::FlushBuffer( Boolean allocateNew )
  54. {
  55.     OSErr    err = noErr;
  56.     
  57.     if ( !fUseBuffer )
  58.         return err;
  59.  
  60.     if ( fBuffer && ( fLastWritten > 0 ) )
  61.     {
  62.         HLock( fBuffer );
  63.         Try_    
  64.         {
  65.             err = LFileStream::PutBytes( *fBuffer, fLastWritten );
  66.             ThrowIfOSErr_(err);
  67.             HUnlock( fBuffer );
  68.         }
  69.         Catch_(inErr)
  70.         {
  71.             HUnlock( fBuffer );
  72.             DisposeHandle(fBuffer);
  73.             fBuffer = NULL;
  74.             fWriteFailed = TRUE;
  75.         }
  76.         EndCatch_
  77.         fLastWritten = 0;
  78.     }
  79.     
  80.     if (fWriteFailed)
  81.         return err;
  82.  
  83.     if ( allocateNew && ( !fBuffer ) )
  84.     {
  85.         fBuffer = ::NewHandle( STREAM_BUFFER_SIZE );
  86.         fBufferSize = STREAM_BUFFER_SIZE;
  87.         fLastWritten = 0;
  88.     }
  89.  
  90.     if ( !allocateNew && fBuffer )
  91.     {
  92.         DisposeHandle( fBuffer );
  93.         fBuffer = NULL;
  94.     }
  95.     
  96.     return err;
  97. }
  98.  
  99. Int32 LFileBufferStream::ReadData( void* outBuffer, Int32 inByteCount )
  100. {
  101.     return LFileStream::ReadData( outBuffer, inByteCount );
  102. }
  103.  
  104. void LFileBufferStream::DoUseBuffer()
  105. {
  106.     fUseBuffer = TRUE;
  107. }
  108.  
  109. void LFileBufferStream::CloseDataFork()
  110. {
  111.     FlushBuffer( FALSE );
  112.     LFileStream::CloseDataFork();
  113. }
  114.  
  115. Int32 LFileBufferStream::WriteData( const void* inFromBuffer, Int32 inByteCount )
  116. {
  117.     OSErr err = noErr;
  118.     
  119.     if ( fUseBuffer && ( fLastWritten + inByteCount ) > fBufferSize )
  120.         err = FlushBuffer( TRUE );
  121.         
  122.     ThrowIfOSErr_(err);
  123.  
  124.     if ( ( fBuffer ) &&     // If we have space, fill up the buffer
  125.         ( ( fLastWritten + inByteCount ) <= fBufferSize ) )
  126.     {
  127.         ::BlockMoveData( inFromBuffer, &( (*fBuffer)[fLastWritten] ), inByteCount );
  128.         fLastWritten += inByteCount;
  129.         return inByteCount;
  130.     }
  131.     // Otherwise, just do a normal write
  132.     else
  133.     {
  134.         err = LFileStream::PutBytes( inFromBuffer, inByteCount );
  135.         ThrowIfOSErr_(err);
  136.     }
  137.  
  138.     return inByteCount;
  139. }
  140.  
  141.  
  142. #ifdef PROFILE
  143. #pragma profile off
  144. #endif
  145.