View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import java.io.File;
14  import java.io.FileNotFoundException;
15  import java.io.IOException;
16  import java.util.List;
17  
18  import org.eclipse.jgit.internal.storage.pack.CachedPack;
19  import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
20  import org.eclipse.jgit.internal.storage.pack.PackExt;
21  import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
22  import org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation;
23  
24  class LocalCachedPack extends CachedPack {
25  	private final ObjectDirectory odb;
26  
27  	private final String[] packNames;
28  
29  	private Pack[] packs;
30  
31  	LocalCachedPack(ObjectDirectory odb, List<String> packNames) {
32  		this.odb = odb;
33  		this.packNames = packNames.toArray(new String[0]);
34  	}
35  
36  	LocalCachedPack(List<Pack> packs) {
37  		odb = null;
38  		packNames = null;
39  		this.packs = packs.toArray(new Pack[0]);
40  	}
41  
42  	/** {@inheritDoc} */
43  	@Override
44  	public long getObjectCount() throws IOException {
45  		long cnt = 0;
46  		for (Pack pack : getPacks())
47  			cnt += pack.getObjectCount();
48  		return cnt;
49  	}
50  
51  	void copyAsIs(PackOutputStream out, WindowCursor wc)
52  			throws IOException {
53  		for (Pack pack : getPacks())
54  			pack.copyPackAsIs(out, wc);
55  	}
56  
57  	/** {@inheritDoc} */
58  	@Override
59  	public boolean hasObject(ObjectToPack obj, StoredObjectRepresentation rep) {
60  		try {
61  			LocalObjectRepresentation local = (LocalObjectRepresentation) rep;
62  			for (Pack pack : getPacks()) {
63  				if (local.pack == pack)
64  					return true;
65  			}
66  			return false;
67  		} catch (FileNotFoundException packGone) {
68  			return false;
69  		}
70  	}
71  
72  	private Pack[] getPacks() throws FileNotFoundException {
73  		if (packs == null) {
74  			Pack[] p = new Pack[packNames.length];
75  			for (int i = 0; i < packNames.length; i++)
76  				p[i] = getPackFile(packNames[i]);
77  			packs = p;
78  		}
79  		return packs;
80  	}
81  
82  	private Pack getPackFile(String packName) throws FileNotFoundException {
83  		for (Pack pack : odb.getPacks()) {
84  			if (packName.equals(pack.getPackName()))
85  				return pack;
86  		}
87  		throw new FileNotFoundException(getPackFilePath(packName));
88  	}
89  
90  	private String getPackFilePath(String packName) {
91  		final File packDir = odb.getPackDirectory();
92  		return new PackFile(packDir, packName, PackExt.PACK).getPath();
93  	}
94  }