Initial EGit contribution to eclipse.org
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / internal / storage / BlobStorage.java
blob05aa0edf6335cf0621ccfa58aee98bb4276b22fd
1 /*******************************************************************************
2 * Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *******************************************************************************/
10 package org.eclipse.egit.core.internal.storage;
12 import java.io.ByteArrayInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
16 import org.eclipse.core.internal.resources.ResourceException;
17 import org.eclipse.core.resources.IResourceStatus;
18 import org.eclipse.core.resources.IStorage;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
23 import org.eclipse.jgit.lib.Constants;
24 import org.eclipse.jgit.lib.ObjectId;
25 import org.eclipse.jgit.lib.ObjectLoader;
26 import org.eclipse.jgit.lib.Repository;
28 /** Accesses a blob from Git. */
29 class BlobStorage implements IStorage {
30 private final Repository db;
32 private final String path;
34 private final ObjectId blobId;
36 BlobStorage(final Repository repository, final String fileName,
37 final ObjectId blob) {
38 db = repository;
39 path = fileName;
40 blobId = blob;
43 public InputStream getContents() throws CoreException {
44 try {
45 return open();
46 } catch (IOException e) {
47 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
48 getFullPath(), "IO error reading Git blob " + blobId + ".",
49 e);
53 private InputStream open() throws IOException, ResourceException,
54 IncorrectObjectTypeException {
55 final ObjectLoader reader = db.openBlob(blobId);
56 if (reader == null)
57 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
58 getFullPath(), "Git blob " + blobId + " not found.", null);
59 final byte[] data = reader.getBytes();
60 if (reader.getType() != Constants.OBJ_BLOB)
61 throw new IncorrectObjectTypeException(blobId, Constants.TYPE_BLOB);
62 return new ByteArrayInputStream(data);
65 public IPath getFullPath() {
66 return Path.fromPortableString(path);
69 public String getName() {
70 final int last = path.lastIndexOf('/');
71 return last >= 0 ? path.substring(last + 1) : path;
74 public boolean isReadOnly() {
75 return true;
78 public Object getAdapter(final Class adapter) {
79 return null;