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