1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.stream.PipedStream;
13 
14 import hunt.stream.Common;
15 import hunt.stream.ByteArrayInputStream;
16 import hunt.stream.ByteArrayOutputStream;
17 import hunt.stream.BufferedInputStream;
18 import hunt.stream.BufferedOutputStream;
19 import hunt.stream.FileInputStream;
20 import hunt.stream.FileOutputStream;
21 import hunt.util.Common;
22 
23 // import hunt.logging;
24 
25 import std.path;
26 import std.file;
27 
28 interface PipedStream : Closeable {
29 
30     InputStream getInputStream();
31 
32     OutputStream getOutputStream();
33 }
34 
35 /**
36 */
37 class ByteArrayPipedStream : PipedStream {
38 
39     private ByteArrayOutputStream outStream;
40     // private ByteArrayInputStream inStream;
41     private int size;
42 
43     this(int size) {
44         this.size = size;
45     }
46 
47     void close() {
48         // inStream = null;
49         outStream = null;
50     }
51 
52     ByteArrayInputStream getInputStream() {
53         return new ByteArrayInputStream(outStream.toByteArray());
54         // if (inStream is null) {
55         //     inStream = new ByteArrayInputStream(outStream.toByteArray());
56         //     // outStream = null;
57         // }
58         // return inStream;
59     }
60 
61     ByteArrayOutputStream getOutputStream() {
62         if (outStream is null) {
63             outStream = new ByteArrayOutputStream(size);
64         }
65         return outStream;
66     }
67 }
68 
69 /**
70  * 
71  */
72 class FilePipedStream : PipedStream {
73 
74     private BufferedOutputStream output;
75     // private BufferedInputStream input;
76     private string temp;
77 
78     this() {
79         this(tempDir());
80     }
81 
82     this(string tempdir) {
83         import std.uuid;
84         temp = tempdir  ~ dirSeparator ~ "hunt-" ~ randomUUID().toString();
85     }
86 
87     void close() {
88         import std.array;
89         if (temp.empty())
90             return;
91 
92         try {
93             temp.remove();
94         } finally {
95             // if (input !is null)
96             //     input.close();
97 
98             if (output !is null)
99                 output.close();
100         }
101 
102         // input = null;
103         output = null;
104         temp = null;
105     }
106 
107 
108     BufferedInputStream getInputStream() {
109         // if (input is null) {
110         //     input = new BufferedInputStream(new FileInputStream(temp));
111         // }
112         // return input;
113         return new BufferedInputStream(new FileInputStream(temp));
114     }
115 
116     BufferedOutputStream getOutputStream() {
117         if (output is null) {
118             output = new BufferedOutputStream(new FileOutputStream(temp));
119         }
120         return output;
121     }
122 }
123