Changeset 1101


Ignore:
Timestamp:
Jan 20, 2010, 6:57:29 PM (12 years ago)
Author:
Matthäus Wander
Message:

CStream:

  • added another ReadFully() overload + test case
  • added compatibility to act like a readable CryptoolStream
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPluginBase/IO/CStreamReader.cs

    r1091 r1101  
    3939        private bool _disposed;
    4040
     41        [Obsolete("for more or less clean disposal of obsolete self-created CryptoolStream instances")]
     42        private List<CryptoolStream> disposeStreams = new List<CryptoolStream>();
     43
    4144        public CStream(CStreamWriter writer)
    4245        {
     
    144147        /// Read POSIX-like 1 to count amount of bytes into given byte array.
    145148        /// Blocks until at least 1 byte has been read or underlying stream has been closed.
    146         /// Does not guarantee to read the requested amount of data, can read less.
     149        /// Does not guarantee to read the requested/available amount of data, can read less.
    147150        /// </summary>
    148151        /// <param name="buffer"></param>
     
    183186
    184187        /// <summary>
    185         /// Convenience method for Read (non-POSIX): block until array is full or EOF occurs.
     188        /// Convenience method for Read: read and block until EOF occurs.
     189        ///
     190        /// This method is inefficient for large data amounts. You should avoid it in stable code.
     191        /// </summary>
     192        public byte[] ReadFully()
     193        {
     194            List<byte[]> list = new List<byte[]>();
     195            int overall = 0;
     196
     197            { // read list of buffers
     198                byte[] buf;
     199                int read;
     200                do
     201                {
     202                    buf = new byte[4096];
     203                    read = ReadFully(buf);
     204
     205                    if (read > 0)
     206                    {
     207                        if (read < buf.Length)
     208                        { // special case: read less bytes than buffer can hold
     209                            byte[] resizedBuf = new byte[read];
     210                            Array.Copy(buf, resizedBuf, read);
     211                            list.Add(resizedBuf);
     212                        }
     213                        else
     214                        { // default case
     215                            Debug.Assert(buf.Length == read);
     216                            list.Add(buf);
     217                        }
     218
     219                        overall += read;
     220                    }
     221                } while (read == buf.Length); // not EOF
     222            }
     223
     224            { // concat buffers to bigbuffer
     225                byte[] bigbuffer = new byte[overall];
     226                int offset = 0;
     227                foreach (byte[] buf in list)
     228                {
     229                    Array.Copy(buf, 0, bigbuffer, offset, buf.Length);
     230                    offset += buf.Length;
     231                }
     232
     233                Debug.Assert(offset == overall);
     234
     235                return bigbuffer;
     236            }
     237        }
     238
     239        /// <summary>
     240        /// Convenience method for Read: read and block until array is full or EOF occurs.
    186241        /// </summary>
    187242        public int ReadFully(byte[] buffer)
     
    191246
    192247        /// <summary>
    193         /// Convenience method for Read (non-POSIX): block until required amount of data has
     248        /// Convenience method for Read: read and block until required amount of data has
    194249        /// been retrieved or EOF occurs.
    195250        /// </summary>
     
    231286        }
    232287
     288        /// <summary>
     289        /// Compatibility with obsolete CryptoolStream. This method has not been tested thoroughly.
     290        /// If it breaks, upgrade your plugin to use the new CStream natively.
     291        /// </summary>
     292        /// <returns></returns>
     293        [Obsolete("Replace usage of CryptoolStream with CStream")]
     294        public CryptoolStream ReadCryptoolStream()
     295        {
     296            // CryptoolStream requires a file to read
     297            _writer.EnsureSwap();
     298
     299            CryptoolStream cs = new CryptoolStream();
     300            cs.OpenRead(_writer.FilePath);
     301            disposeStreams.Add(cs);
     302            return cs;
     303        }
     304
    233305        #endregion
    234306
     
    279351            _writer.ShutdownEvent -= shutdownHandler;
    280352            _writer.SwapEvent -= swapHandler;
     353
     354            // disposal of obsolete CryptoolStreams
     355            foreach (CryptoolStream cs in disposeStreams)
     356            {
     357                cs.Dispose();
     358            }
    281359
    282360            _disposed = true;
  • trunk/CrypPluginBase/IO/CStreamWriter.cs

    r1091 r1101  
    200200        }
    201201
    202         public void Dispose()
     202        public new void Dispose()
    203203        {
    204204            if (!_disposed)
     
    277277                else
    278278                {
    279                     if (!IsSwapped)
    280                     {
    281                         createSwapFile();
    282                         _writeStream.Write(_buffer, 0, _bufPtr);
    283                         _writeStream.Flush(); // ensure reader can seek before announcing swap event
    284                         _buffer = null;
    285 
    286                         if (SwapEvent != null)
    287                             SwapEvent();
    288                     }
     279                    EnsureSwap();
    289280
    290281                    _writeStream.Write(buf, offset, count);
     
    360351        }
    361352
     353        internal void EnsureSwap()
     354        {
     355            if (!IsSwapped)
     356            {
     357                createSwapFile();
     358                _writeStream.Write(_buffer, 0, _bufPtr);
     359                _writeStream.Flush(); // ensure reader can seek before announcing swap event
     360                _buffer = null;
     361
     362                if (SwapEvent != null)
     363                    SwapEvent();
     364            }
     365        }
     366
    362367        #endregion
    363368
  • trunk/CrypPluginBase/IO/CryptoolStream.cs

    r1081 r1101  
    108108                // finalization and public dispose code
    109109                isDisposed = true;
     110                Close();
    110111
    111112                if (deleteOnDispose)
  • trunk/DevTestMethods/CrypPluginBase/CStream.cs

    r1091 r1101  
    259259            Assert.IsFalse(File.Exists(filePath));
    260260        }
     261
     262        [TestMethod]
     263        public void TestExhaustiveRead()
     264        {
     265            CStreamWriter writer = new CStreamWriter();
     266
     267            writer.Write(LongData);
     268            writer.Write(LongData);
     269            writer.Write(LongData);
     270            writer.Close();
     271
     272            CStream reader = writer.CreateReader();
     273            byte[] bigbuf = reader.ReadFully();
     274
     275            Assert.AreEqual(LongData.Length * 3, bigbuf.Length);
     276        }
    261277    }
    262278}
Note: See TracChangeset for help on using the changeset viewer.