1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.config;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.util.Objects;
28
29
30
31
32 public class ConfigurationSource {
33 public static final ConfigurationSource NULL_SOURCE = new ConfigurationSource(new byte[0]);
34
35 private final File file;
36 private final URL url;
37 private final String location;
38 private final InputStream stream;
39 private final byte[] data;
40
41
42
43
44
45
46
47
48 private static byte[] toByteArray(final InputStream inputStream) throws IOException {
49 final int buffSize = Math.max(4096, inputStream.available());
50 final ByteArrayOutputStream contents = new ByteArrayOutputStream(buffSize);
51 final byte[] buff = new byte[buffSize];
52
53 int length = inputStream.read(buff);
54 while (length > 0) {
55 contents.write(buff, 0, length);
56 length = inputStream.read(buff);
57 }
58 return contents.toByteArray();
59 }
60
61
62
63
64
65
66
67
68 public ConfigurationSource(final InputStream stream) throws IOException {
69 this(toByteArray(stream));
70 }
71
72 private ConfigurationSource(final byte[] data) {
73 this.data = Objects.requireNonNull(data, "data is null");
74 this.stream = new ByteArrayInputStream(data);
75 this.file = null;
76 this.url = null;
77 this.location = null;
78 }
79
80
81
82
83
84
85
86
87 public ConfigurationSource(final InputStream stream, final File file) {
88 this.stream = Objects.requireNonNull(stream, "stream is null");
89 this.file = Objects.requireNonNull(file, "file is null");
90 this.location = file.getAbsolutePath();
91 this.url = null;
92 this.data = null;
93 }
94
95
96
97
98
99
100
101
102 public ConfigurationSource(final InputStream stream, final URL url) {
103 this.stream = Objects.requireNonNull(stream, "stream is null");
104 this.url = Objects.requireNonNull(url, "URL is null");
105 this.location = url.toString();
106 this.file = null;
107 this.data = null;
108 }
109
110
111
112
113
114
115
116 public File getFile() {
117 return file;
118 }
119
120
121
122
123
124
125
126 public URL getURL() {
127 return url;
128 }
129
130
131
132
133
134
135
136 public String getLocation() {
137 return location;
138 }
139
140
141
142
143
144
145 public InputStream getInputStream() {
146 return stream;
147 }
148
149
150
151
152
153
154
155 public ConfigurationSource resetInputStream() throws IOException {
156 if (file != null) {
157 return new ConfigurationSource(new FileInputStream(file), file);
158 } else if (url != null) {
159 return new ConfigurationSource(url.openStream(), url);
160 } else {
161 return new ConfigurationSource(data);
162 }
163 }
164
165 @Override
166 public String toString() {
167 if (location != null) {
168 return location;
169 }
170 if (this == NULL_SOURCE) {
171 return "NULL_SOURCE";
172 }
173 final int length = data == null ? -1 : data.length;
174 return "stream (" + length + " bytes, unknown location)";
175 }
176 }