1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.javadpkg.io.impl;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import net.sourceforge.javadpkg.io.DataSource;
25
26
27
28
29
30
31
32
33
34 public class DataStreamSource implements DataSource {
35
36
37
38 private InputStream in;
39
40
41
42
43
44
45 private UncloseableInputStream publicIn;
46
47 private String name;
48
49 private boolean closeable;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public DataStreamSource(InputStream in, String name, boolean closeable) {
67 super();
68
69 if (in == null)
70 throw new IllegalArgumentException("Argument in is null.");
71 if (name == null)
72 throw new IllegalArgumentException("Argument name is null.");
73
74 this.in = in;
75 this.name = name;
76 this.closeable = closeable;
77
78 this.createPublicInputStream();
79 }
80
81
82 @Override
83 public String getName() {
84 return this.name;
85 }
86
87
88 @Override
89 public long getLength() {
90 return -1;
91 }
92
93
94 @Override
95 public boolean isResettable() {
96 return false;
97 }
98
99
100 @Override
101 public void reset() throws IOException {
102 throw new IOException("Source |" + this.name + "| doesn't support a reset.");
103 }
104
105
106
107
108
109
110
111
112 private void createPublicInputStream() {
113 this.publicIn = new UncloseableInputStream(this.in, new DelegateCloseHandler(this));
114 }
115
116
117 @Override
118 public InputStream getInputStream() throws IOException {
119 return this.publicIn;
120 }
121
122
123 @Override
124 public void close() throws IOException {
125 try {
126 if ((this.in != null) && this.closeable) {
127 this.in.close();
128 }
129 } finally {
130 this.publicIn = null;
131 this.in = null;
132 }
133 }
134
135
136 }