You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LfsBlobLoader.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2017, Markus Duft <markus.duft@ssi-schaefer.com> 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. package org.eclipse.jgit.lfs;
  11. import java.io.IOException;
  12. import java.nio.file.Files;
  13. import java.nio.file.Path;
  14. import java.nio.file.attribute.BasicFileAttributes;
  15. import org.eclipse.jgit.errors.LargeObjectException;
  16. import org.eclipse.jgit.errors.MissingObjectException;
  17. import org.eclipse.jgit.lib.Constants;
  18. import org.eclipse.jgit.lib.ObjectLoader;
  19. import org.eclipse.jgit.lib.ObjectStream;
  20. import org.eclipse.jgit.storage.pack.PackConfig;
  21. import org.eclipse.jgit.util.IO;
  22. /**
  23. * An {@link ObjectLoader} implementation that reads a media file from the LFS
  24. * storage.
  25. *
  26. * @since 4.11
  27. */
  28. public class LfsBlobLoader extends ObjectLoader {
  29. private Path mediaFile;
  30. private BasicFileAttributes attributes;
  31. private byte[] cached;
  32. /**
  33. * Create a loader for the LFS media file at the given path.
  34. *
  35. * @param mediaFile
  36. * path to the file
  37. * @throws IOException
  38. * in case of an error reading attributes
  39. */
  40. public LfsBlobLoader(Path mediaFile) throws IOException {
  41. this.mediaFile = mediaFile;
  42. this.attributes = Files.readAttributes(mediaFile,
  43. BasicFileAttributes.class);
  44. }
  45. @Override
  46. public int getType() {
  47. return Constants.OBJ_BLOB;
  48. }
  49. @Override
  50. public long getSize() {
  51. return attributes.size();
  52. }
  53. @Override
  54. public byte[] getCachedBytes() throws LargeObjectException {
  55. if (getSize() > PackConfig.DEFAULT_BIG_FILE_THRESHOLD) {
  56. throw new LargeObjectException();
  57. }
  58. if (cached == null) {
  59. try {
  60. cached = IO.readFully(mediaFile.toFile());
  61. } catch (IOException ioe) {
  62. throw new LargeObjectException(ioe);
  63. }
  64. }
  65. return cached;
  66. }
  67. @Override
  68. public ObjectStream openStream()
  69. throws MissingObjectException, IOException {
  70. return new ObjectStream.Filter(getType(), getSize(),
  71. Files.newInputStream(mediaFile));
  72. }
  73. }