TAMAYA-198: Reading input stream into resettable ByteArrayInputStream, so each format read can be explicitly reset and remote resources are read only once.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/b9299af4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/b9299af4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/b9299af4 Branch: refs/heads/master Commit: b9299af45221d0a6cb977d0823643a47a66d8f39 Parents: b6be117 Author: anatole <anat...@apache.org> Authored: Mon Dec 19 23:38:26 2016 +0100 Committer: anatole <anat...@apache.org> Committed: Mon Dec 19 23:38:26 2016 +0100 ---------------------------------------------------------------------- .../BaseFormatPropertySourceProvider.java | 35 +++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b9299af4/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java index 90e1143..49f05df 100644 --- a/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java +++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java @@ -21,6 +21,8 @@ package org.apache.tamaya.format; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertySourceProvider; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -138,12 +140,35 @@ public abstract class BaseFormatPropertySourceProvider implements PropertySource @Override public Collection<PropertySource> getPropertySources() { List<PropertySource> propertySources = new ArrayList<>(); + byte[] buff = new byte[512]; for (URL res : this.paths) { - for (ConfigurationFormat format : configFormats) { - try(InputStream is = res.openStream()) { - if (format.accepts(res)) { - ConfigurationData data = format.readConfiguration(res.toString(), is); - propertySources.addAll(getPropertySources(data)); + byte[] dataBytes = null; + int read = 0; + try(InputStream is = res.openStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream();) { + read = is.read(buff); + while (read > 0) { + bos.write(buff, 0, read); + read = is.read(buff); + } + bos.flush(); + dataBytes = bos.toByteArray(); + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to read resource based config: " + res, e); + } + if(dataBytes!=null) { + try (InputStream is = new ByteArrayInputStream(dataBytes);) { + for (ConfigurationFormat format : configFormats) { + try { + if (format.accepts(res)) { + ConfigurationData data = format.readConfiguration(res.toString(), is); + propertySources.addAll(getPropertySources(data)); + } + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e); + } finally { + is.reset(); + } } } catch (Exception e) { LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e);