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