Temporarily include bndlib 1.47 for testing purposes (not yet on central)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1185095 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/lib/io/IO.java b/bundleplugin/src/main/java/aQute/lib/io/IO.java
new file mode 100644
index 0000000..d2cb411
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/io/IO.java
@@ -0,0 +1,162 @@
+package aQute.lib.io;
+
+import java.io.*;
+import java.net.*;
+import java.nio.*;
+import java.util.*;
+
+public class IO {
+ public static void copy(InputStream in, OutputStream out) throws IOException {
+ DataOutputStream dos = new DataOutputStream(out);
+ copy(in, (DataOutput) dos);
+ }
+
+ public static void copy(InputStream in, DataOutput out) throws IOException {
+ byte[] buffer = new byte[10000];
+ try {
+ int size = in.read(buffer);
+ while (size > 0) {
+ out.write(buffer, 0, size);
+ size = in.read(buffer);
+ }
+ } finally {
+ in.close();
+ }
+ }
+
+ public static void copy(InputStream in, ByteBuffer bb) throws IOException {
+ byte[] buffer = new byte[10000];
+ try {
+ int size = in.read(buffer);
+ while (size > 0) {
+ bb.put(buffer, 0, size);
+ size = in.read(buffer);
+ }
+ } finally {
+ in.close();
+ }
+ }
+
+ public static void copy(File a, File b) throws IOException {
+ FileOutputStream out = new FileOutputStream(b);
+ try {
+ copy(new FileInputStream(a), out);
+ } finally {
+ out.close();
+ }
+ }
+
+ public static void copy(InputStream a, File b) throws IOException {
+ FileOutputStream out = new FileOutputStream(b);
+ try {
+ copy(a, out);
+ } finally {
+ out.close();
+ }
+ }
+
+ public static void copy(File a, OutputStream b) throws IOException {
+ copy(new FileInputStream(a), b);
+ }
+
+ public static String collect(File a, String encoding) throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ copy(a, out);
+ return new String(out.toByteArray(), encoding);
+ }
+
+ public static String collect(URL a, String encoding) throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ copy(a.openStream(), out);
+ return new String(out.toByteArray(), encoding);
+ }
+
+ public static String collect(File a) throws IOException {
+ return collect(a, "UTF-8");
+ }
+
+ public static String collect(InputStream a, String encoding) throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ copy(a, out);
+ return new String(out.toByteArray(), encoding);
+ }
+
+ public static String collect(InputStream a) throws IOException {
+ return collect(a, "UTF-8");
+ }
+
+ public static String collect(Reader a) throws IOException {
+ StringWriter sw = new StringWriter();
+ char[] buffer = new char[10000];
+ int size = a.read(buffer);
+ while (size > 0) {
+ sw.write(buffer, 0, size);
+ size = a.read(buffer);
+ }
+ return sw.toString();
+ }
+
+ public static File getFile(File base, String file) {
+ File f = new File(file);
+ if (f.isAbsolute())
+ return f;
+ int n;
+
+ f = base.getAbsoluteFile();
+ while ((n = file.indexOf('/')) > 0) {
+ String first = file.substring(0, n);
+ file = file.substring(n + 1);
+ if (first.equals(".."))
+ f = f.getParentFile();
+ else
+ f = new File(f, first);
+ }
+ if (file.equals(".."))
+ return f.getParentFile();
+ else
+ return new File(f, file).getAbsoluteFile();
+ }
+
+ public static void delete(File f) {
+ f = f.getAbsoluteFile();
+ if (f.getParentFile() == null)
+ throw new IllegalArgumentException("Cannot recursively delete root for safety reasons");
+
+ if (f.isDirectory()) {
+ File[] subs = f.listFiles();
+ for (File sub : subs)
+ delete(sub);
+ }
+
+ f.delete();
+ }
+
+ public static void drain(InputStream in) throws IOException {
+ byte[] buffer = new byte[10000];
+ try {
+ int size = in.read(buffer);
+ while (size > 0) {
+ size = in.read(buffer);
+ }
+ } finally {
+ in.close();
+ }
+ }
+
+ public void copy(Collection<?> c, OutputStream out) {
+ PrintStream ps = new PrintStream(out);
+ for (Object o : c) {
+ ps.println(o);
+ }
+ ps.flush();
+ }
+
+ public static Throwable close(Closeable in) {
+ try {
+ in.close();
+ return null;
+ } catch (Throwable e) {
+ return e;
+ }
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/lib/io/LimitedInputStream.java b/bundleplugin/src/main/java/aQute/lib/io/LimitedInputStream.java
new file mode 100644
index 0000000..bf9a21f
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/io/LimitedInputStream.java
@@ -0,0 +1,78 @@
+package aQute.lib.io;
+
+import java.io.*;
+
+public class LimitedInputStream extends InputStream {
+
+ final InputStream in;
+ final int size;
+ int left;
+
+ public LimitedInputStream(InputStream in, int size) {
+ this.in = in;
+ this.left = size;
+ this.size = size;
+ }
+
+ @Override public int read() throws IOException {
+ if (left <= 0) {
+ eof();
+ return -1;
+ }
+
+ left--;
+ return in.read();
+ }
+
+ @Override public int available() throws IOException {
+ return Math.min(left, in.available());
+ }
+
+ @Override public void close() throws IOException {
+ eof();
+ in.close();
+ }
+
+ protected void eof() {
+ }
+
+ @Override public synchronized void mark(int readlimit) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override public boolean markSupported() {
+ return false;
+ }
+
+ @Override public int read(byte[] b, int off, int len) throws IOException {
+ int min = Math.min(len, left);
+ if (min == 0)
+ return 0;
+
+ int read = in.read(b, off, min);
+ if (read > 0)
+ left -= read;
+ return read;
+ }
+
+ @Override public int read(byte[] b) throws IOException {
+ return read(b,0,b.length);
+ }
+
+ @Override public synchronized void reset() throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override public long skip(long n) throws IOException {
+ long count = 0;
+ byte buffer[] = new byte[1024];
+ while ( n > 0 && read() >= 0) {
+ int size = read(buffer);
+ if ( size <= 0)
+ return count;
+ count+=size;
+ n-=size;
+ }
+ return count;
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/lib/io/packageinfo b/bundleplugin/src/main/java/aQute/lib/io/packageinfo
new file mode 100644
index 0000000..7c8de03
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/io/packageinfo
@@ -0,0 +1 @@
+version 1.0