Fix incorrect editor updates with changes from diff-viewer
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / internal / storage / BlobStorage.java
blobcd30bc2d0688449ca31d2dce0621f45184015e30
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.egit.core.CoreText;
23 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
24 import org.eclipse.jgit.lib.Constants;
25 import org.eclipse.jgit.lib.ObjectId;
26 import org.eclipse.jgit.lib.ObjectLoader;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.osgi.util.NLS;
30 /** Accesses a blob from Git. */
31 class BlobStorage implements IStorage {
32 protected final Repository db;
34 private final String path;
36 private final ObjectId blobId;
38 BlobStorage(final Repository repository, final String fileName,
39 final ObjectId blob) {
40 db = repository;
41 path = fileName;
42 blobId = blob;
45 public InputStream getContents() throws CoreException {
46 try {
47 return open();
48 } catch (IOException e) {
49 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
50 getFullPath(), NLS.bind(
51 CoreText.BlobStorage_errorReadingBlob, blobId), e);
55 private InputStream open() throws IOException, ResourceException,
56 IncorrectObjectTypeException {
57 final ObjectLoader reader = db.openBlob(blobId);
58 if (reader == null)
59 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
60 getFullPath(), NLS.bind(CoreText.BlobStorage_blobNotFound,
61 blobId), null);
62 final byte[] data = reader.getBytes();
63 if (reader.getType() != Constants.OBJ_BLOB)
64 throw new IncorrectObjectTypeException(blobId, Constants.TYPE_BLOB);
65 return new ByteArrayInputStream(data);
68 public IPath getFullPath() {
69 return Path.fromPortableString(path);
72 public String getName() {
73 final int last = path.lastIndexOf('/');
74 return last >= 0 ? path.substring(last + 1) : path;
77 public boolean isReadOnly() {
78 return true;
81 public Object getAdapter(final Class adapter) {
82 return null;