From f1f8c0eb0b14bde223b2978cf4afcfd8cabf7a1d Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Thu, 2 Dec 2010 19:45:58 -0500 Subject: [PATCH] Format for types that can be read/written to/from InputStream/OutputStream use case: java.util.jar.Manifest --- cache/Cache.scala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cache/Cache.scala b/cache/Cache.scala index 238773b18..21e89abc4 100644 --- a/cache/Cache.scala +++ b/cache/Cache.scala @@ -4,7 +4,7 @@ package sbt import sbinary.{CollectionTypes, DefaultProtocol, Format, Input, JavaFormats, Output} -import java.io.File +import java.io.{ByteArrayInputStream, ByteArrayOutputStream, File, InputStream, OutputStream} import java.net.{URI, URL} import Types.:+: import DefaultProtocol.{asProduct2, asSingleton, BooleanFormat, ByteFormat, IntFormat, wrap} @@ -108,7 +108,13 @@ trait BasicCacheImplicits implicit def stringSetEquiv: Equiv[Set[String]] = defaultEquiv implicit def stringMapEquiv: Equiv[Map[String, String]] = defaultEquiv - + def streamFormat[T](write: (T, OutputStream) => Unit, f: InputStream => T): Format[T] = + { + val toBytes = (t: T) => { val bos = new ByteArrayOutputStream; write(t, bos); bos.toByteArray } + val fromBytes = (bs: Array[Byte]) => f(new ByteArrayInputStream(bs)) + wrap(toBytes, fromBytes)(DefaultProtocol.ByteArrayFormat) + } + implicit def xmlInputCache(implicit strEq: InputCache[String]): InputCache[NodeSeq] = wrapIn[NodeSeq, String](_.toString, strEq) implicit def seqCache[T](implicit t: InputCache[T]): InputCache[Seq[T]] =