0

The API I'm working with takes in a com.google.common.io.ByteSource. My API takes in a javax.xml.transform.Source since I'm doing Schema validation as well as calling the other API. Is there a clean way to convert from a Source to a ByteSource and/or there is a better object to take in that would meet both methods' needs (i.e. ByteSource and Source).

Joe W
  • 1,789
  • 3
  • 28
  • 42
  • Sounds like a job for the adapter pattern: http://en.wikipedia.org/wiki/Adapter_pattern – Durandal Sep 30 '14 at 19:03
  • Related: http://stackoverflow.com/questions/3711239/how-to-convert-javax-xml-transform-source-into-an-inputstream – ColinD Sep 30 '14 at 19:04

2 Answers2

3

ByteSource and Source don't seem like particularly compatible APIs:

  • Source doesn't appear to be a source of bytes, in general. There are multiple implementations and only one can (StreamSource) provides an InputStream for reading bytes.
  • Even StreamSource does not appear to be compatible with the expected contract of a ByteSource. It looks like StreamSource is basically a wrapper around a single InputStream and/or Reader, while ByteSource is expected to be able to return a new, independent InputStream each time openStream() is called.

Is there not a version of the API you're working with that takes an InputStream rather than a ByteSource? It's typical (or should be, anyway) for there to exist both versions of a method.

ColinD
  • 108,630
  • 30
  • 201
  • 202
1

ByteSource can give an InputStream which can be used for the transformation using ByteSource.openBufferedStream.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I ended up changing my API to take in a ByteSource and went with this approach. Not the prettiest, but it works. Thanks. – Joe W Sep 30 '14 at 19:35