Initial set of files.
[egit.git] / src / org / spearce / jgit / lib / FileCopier.java
blob199607c2e57a9ed7497047cf5ced33f4fcdbc2e3
1 package org.spearce.jgit.lib;
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Iterator;
9 public class FileCopier {
10 public void copyOut(final Tree src, final File dest) throws IOException {
11 dest.mkdirs();
12 copyOut(src, dest, new byte[8192]);
15 protected void copyOut(final Tree src, final File dest,
16 final byte[] copyBuffer) throws IOException {
17 final Iterator i;
19 i = src.getTreeEntries().iterator();
20 while (i.hasNext()) {
21 final Tree.Entry e = (Tree.Entry) i.next();
22 final File f = new File(dest, e.getName());
24 if (e.isTree()) {
25 f.mkdir();
26 copyOut(e.getTree(), f, copyBuffer);
27 } else if (e.isSymlink()) {
28 // TODO: We don't handle symlinks right now.
29 } else {
30 final ObjectReader or = e.openBlob();
31 final InputStream is;
32 if (or == null) {
33 throw new CorruptObjectException("Missing blob "
34 + e.getId());
36 is = or.getInputStream();
37 try {
38 final FileOutputStream fos = new FileOutputStream(f);
39 try {
40 int r;
41 while ((r = is.read(copyBuffer)) > 0) {
42 fos.write(copyBuffer, 0, r);
44 } finally {
45 fos.close();
47 } finally {
48 or.close();