Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

LargeNonDeltaObject.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.dht;
  44. import java.io.BufferedInputStream;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.util.zip.InflaterInputStream;
  48. import org.eclipse.jgit.errors.LargeObjectException;
  49. import org.eclipse.jgit.errors.MissingObjectException;
  50. import org.eclipse.jgit.generated.storage.dht.proto.GitStore.ChunkMeta;
  51. import org.eclipse.jgit.lib.ObjectLoader;
  52. import org.eclipse.jgit.lib.ObjectStream;
  53. /** Loader for a large non-delta object. */
  54. class LargeNonDeltaObject extends ObjectLoader {
  55. private final int type;
  56. private final long sz;
  57. private final int pos;
  58. private final DhtReader ctx;
  59. private final ChunkMeta meta;
  60. private PackChunk firstChunk;
  61. LargeNonDeltaObject(int type, long sz, PackChunk pc, int pos, DhtReader ctx) {
  62. this.type = type;
  63. this.sz = sz;
  64. this.pos = pos;
  65. this.ctx = ctx;
  66. this.meta = pc.getMeta();
  67. firstChunk = pc;
  68. }
  69. @Override
  70. public boolean isLarge() {
  71. return true;
  72. }
  73. @Override
  74. public byte[] getCachedBytes() throws LargeObjectException {
  75. throw new LargeObjectException.ExceedsByteArrayLimit();
  76. }
  77. @Override
  78. public int getType() {
  79. return type;
  80. }
  81. @Override
  82. public long getSize() {
  83. return sz;
  84. }
  85. @Override
  86. public ObjectStream openStream() throws MissingObjectException, IOException {
  87. PackChunk pc = firstChunk;
  88. if (pc != null)
  89. firstChunk = null;
  90. else
  91. pc = ctx.getChunk(ChunkKey.fromString(meta.getFragment(0)));
  92. InputStream in = new ChunkInputStream(meta, ctx, pos, pc);
  93. in = new BufferedInputStream(new InflaterInputStream(in), 8192);
  94. return new ObjectStream.Filter(type, sz, in);
  95. }
  96. private static class ChunkInputStream extends InputStream {
  97. private final ChunkMeta meta;
  98. private final DhtReader ctx;
  99. private int ptr;
  100. private PackChunk pc;
  101. private int fragment;
  102. ChunkInputStream(ChunkMeta meta, DhtReader ctx, int pos, PackChunk pc) {
  103. this.ctx = ctx;
  104. this.meta = meta;
  105. this.ptr = pos;
  106. this.pc = pc;
  107. }
  108. @Override
  109. public int read(byte[] dstbuf, int dstptr, int dstlen)
  110. throws IOException {
  111. if (0 == dstlen)
  112. return 0;
  113. int n = pc.read(ptr, dstbuf, dstptr, dstlen);
  114. if (n == 0) {
  115. if (fragment == meta.getFragmentCount())
  116. return -1;
  117. pc = ctx.getChunk(ChunkKey.fromString(
  118. meta.getFragment(++fragment)));
  119. ptr = 0;
  120. n = pc.read(ptr, dstbuf, dstptr, dstlen);
  121. if (n == 0)
  122. return -1;
  123. }
  124. ptr += n;
  125. return n;
  126. }
  127. @Override
  128. public int read() throws IOException {
  129. byte[] tmp = new byte[1];
  130. int n = read(tmp, 0, 1);
  131. return n == 1 ? tmp[0] & 0xff : -1;
  132. }
  133. }
  134. }