Line data Source code
1 : // Copyright (C) 2016 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.git; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.protobuf.ByteString; 19 : import java.io.IOException; 20 : import java.io.InputStream; 21 : import org.eclipse.jgit.lib.ObjectId; 22 : import org.eclipse.jgit.lib.ObjectInserter; 23 : import org.eclipse.jgit.lib.ObjectLoader; 24 : 25 : @AutoValue 26 103 : public abstract class InsertedObject { 27 : static InsertedObject create(int type, InputStream in) throws IOException { 28 7 : return create(type, ByteString.readFrom(in)); 29 : } 30 : 31 : static InsertedObject create(int type, ByteString bs) { 32 : ObjectId id; 33 103 : try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) { 34 103 : id = fmt.idFor(type, bs.size(), bs.newInput()); 35 0 : } catch (IOException e) { 36 0 : throw new IllegalStateException(e); 37 103 : } 38 103 : return new AutoValue_InsertedObject(id, type, bs); 39 : } 40 : 41 : static InsertedObject create(int type, byte[] src, int off, int len) { 42 103 : return create(type, ByteString.copyFrom(src, off, len)); 43 : } 44 : 45 : public abstract ObjectId id(); 46 : 47 : public abstract int type(); 48 : 49 : public abstract ByteString data(); 50 : 51 : ObjectLoader newLoader() { 52 23 : return new ObjectLoader.SmallObject(type(), data().toByteArray()); 53 : } 54 : }