vibe.stream.counting 0/76(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
/** Wrapper streams which count the number of bytes or limit the stream based on the number of transferred bytes. Copyright: © 2012 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ module vibe.stream.counting; public import vibe.core.stream; import std.exception; import vibe.internal.interfaceproxy; import vibe.internal.freelistref : FreeListRef; /** Constructs a limited stream from an existing input stream. Params: stream = the input stream to be wrapped byte_limit = the maximum number of bytes readable from the constructed stream silent_limit = if set, the stream will behave exactly like the original stream, but will throw an exception as soon as the limit is reached. */ LimitedInputStream createLimitedInputStream(InputStream)(InputStream stream, ulong byte_limit, bool silent_limit = false) if (isInputStream!InputStream) { return new LimitedInputStream(interfaceProxy!(.InputStream)(stream), byte_limit, silent_limit, true); } /// private FreeListRef!LimitedInputStream createLimitedInputStreamFL(InputStream)(InputStream stream, ulong byte_limit, bool silent_limit = false) if (isInputStream!InputStream) { return FreeListRef!LimitedInputStream(interfaceProxy!(.InputStream)(stream), byte_limit, silent_limit, true); } /** Creates a proxy stream that counts the number of bytes written. Params: output = The stream to forward the written data to byte_limit = Optional total write size limit after which an exception is thrown */ CountingOutputStream createCountingOutputStream(OutputStream)(OutputStream output, ulong byte_limit = ulong.max) if (isOutputStream!OutputStream) { return new CountingOutputStream(interfaceProxy!(.OutputStream)(output), byte_limit, true); } /// private FreeListRef!CountingOutputStream createCountingOutputStreamFL(OutputStream)(OutputStream output, ulong byte_limit = ulong.max) if (isOutputStream!OutputStream) { return FreeListRef!CountingOutputStream(interfaceProxy!(.OutputStream)(output), byte_limit, true); } /** Creates a stream that fires a callback once the end of the underlying input stream is reached. Params: input = Source stream to read from callback = The callback that is invoked one the source stream has been drained */ EndCallbackInputStream createEndCallbackInputStream(InputStream)(InputStream input, void delegate() @safe callback) if (isInputStream!InputStream) { return new EndCallbackInputStream(interfaceProxy!(.InputStream)(input), callback, true); } /// private FreeListRef!EndCallbackInputStream createEndCallbackInputStreamFL(InputStream)(InputStream input, void delegate() @safe callback) if (isInputStream!InputStream) { return FreeListRef!EndCallbackInputStream(interfaceProxy!(.InputStream)(input), callback, true); } /** Wraps an existing stream, limiting the amount of data that can be read. */ class LimitedInputStream : InputStream { @safe: private { InterfaceProxy!InputStream m_input; ulong m_sizeLimit; bool m_silentLimit; } deprecated("Use createLimitedInputStream instead.") this(InputStream stream, ulong byte_limit, bool silent_limit = false) { this(interfaceProxy!InputStream(stream), byte_limit, silent_limit, true); } /// private this(InterfaceProxy!InputStream stream, ulong byte_limit, bool silent_limit, bool dummy) { assert(!!stream); m_input = stream; m_sizeLimit = byte_limit; m_silentLimit = silent_limit; } /// The stream that is wrapped by this one @property inout(InterfaceProxy!InputStream) sourceStream() inout { return m_input; } @property bool empty() { return m_silentLimit ? m_input.empty : (m_sizeLimit == 0); } @property ulong leastSize() { if( m_silentLimit ) return m_input.leastSize; return m_sizeLimit; } @property bool dataAvailableForRead() { return m_input.dataAvailableForRead; } void increment(ulong bytes) { if( bytes > m_sizeLimit ) onSizeLimitReached(); m_sizeLimit -= bytes; } const(ubyte)[] peek() { return m_input.peek(); } size_t read(scope ubyte[] dst, IOMode mode) { if (dst.length > m_sizeLimit) onSizeLimitReached(); auto ret = m_input.read(dst, mode); m_sizeLimit -= ret; return ret; } alias read = InputStream.read; protected void onSizeLimitReached() @safe { throw new LimitException("Size limit reached", m_sizeLimit); } } /** Wraps an existing output stream, counting the bytes that are written. */ class CountingOutputStream : OutputStream { @safe: private { ulong m_bytesWritten; ulong m_writeLimit; InterfaceProxy!OutputStream m_out; } deprecated("Use createCountingOutputStream instead.") this(OutputStream stream, ulong write_limit = ulong.max) { this(interfaceProxy!OutputStream(stream), write_limit, true); } /// private this(InterfaceProxy!OutputStream stream, ulong write_limit, bool dummy) { assert(!!stream); m_writeLimit = write_limit; m_out = stream; } /// Returns the total number of bytes written. @property ulong bytesWritten() const { return m_bytesWritten; } /// The maximum number of bytes to write @property ulong writeLimit() const { return m_writeLimit; } /// ditto @property void writeLimit(ulong value) { m_writeLimit = value; } /** Manually increments the write counter without actually writing data. */ void increment(ulong bytes) { enforce(m_bytesWritten + bytes <= m_writeLimit, "Incrementing past end of output stream."); m_bytesWritten += bytes; } size_t write(in ubyte[] bytes, IOMode mode) { enforce(m_bytesWritten + bytes.length <= m_writeLimit, "Writing past end of output stream."); auto ret = m_out.write(bytes, mode); m_bytesWritten += ret; return ret; } alias write = OutputStream.write; void flush() { m_out.flush(); } void finalize() { m_out.flush(); } } /** Wraps an existing input stream, counting the bytes that are written. */ class CountingInputStream : InputStream { @safe: private { ulong m_bytesRead; InterfaceProxy!InputStream m_in; } deprecated("Use createCountingOutputStream instead.") this(InputStream stream) { this(interfaceProxy!InputStream(stream), true); } /// private this(InterfaceProxy!InputStream stream, bool dummy) { assert(!!stream); m_in = stream; } @property ulong bytesRead() const { return m_bytesRead; } @property bool empty() { return m_in.empty(); } @property ulong leastSize() { return m_in.leastSize(); } @property bool dataAvailableForRead() { return m_in.dataAvailableForRead; } void increment(ulong bytes) { m_bytesRead += bytes; } const(ubyte)[] peek() { return m_in.peek(); } size_t read(scope ubyte[] dst, IOMode mode) { auto ret = m_in.read(dst, mode); m_bytesRead += ret; return ret; } alias read = InputStream.read; } /** Wraps an input stream and calls the given delegate once the stream is empty. Note that this function will potentially block after each read operation to see if the end has already been reached - this may take as long until either new data has arrived or until the connection was closed. The stream will also guarantee that the inner stream is not used after it has been determined to be empty. It can thus be safely deleted once the callback is invoked. */ class EndCallbackInputStream : InputStream { @safe: private { InterfaceProxy!InputStream m_in; bool m_eof = false; void delegate() @safe m_callback; } deprecated("use createEndCallbackInputStream instead.") this(InputStream input, void delegate() @safe callback) { this(interfaceProxy!InputStream(input), callback, true); } /// private this(InterfaceProxy!InputStream input, void delegate() @safe callback, bool dummy) { m_in = input; m_callback = callback; checkEOF(); } @property bool empty() { checkEOF(); return !m_in; } @property ulong leastSize() { checkEOF(); if( m_in ) return m_in.leastSize(); return 0; } @property bool dataAvailableForRead() { if( !m_in ) return false; return m_in.dataAvailableForRead; } const(ubyte)[] peek() { if( !m_in ) return null; return m_in.peek(); } size_t read(scope ubyte[] dst, IOMode mode) { enforce(!!m_in, "Reading past end of stream."); auto ret = m_in.read(dst, mode); checkEOF(); return ret; } alias read = InputStream.read; private void checkEOF() @safe { if( !m_in ) return; if( m_in.empty ){ m_in = InterfaceProxy!InputStream.init; m_callback(); } } } class LimitException : Exception { @safe: private ulong m_limit; this(string message, ulong limit, Throwable next = null, string file = __FILE__, int line = __LINE__) { super(message, next, file, line); } /// The byte limit of the stream that emitted the exception @property ulong limit() const { return m_limit; } }