// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.patch;

import static com.google.gerrit.server.ioutil.BasicSerialization.readBytes;
import static com.google.gerrit.server.ioutil.BasicSerialization.readString;
import static com.google.gerrit.server.ioutil.BasicSerialization.writeBytes;
import static com.google.gerrit.server.ioutil.BasicSerialization.writeString;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

public class DiffSummary implements Serializable {
  private static final long serialVersionUID = PatchListKey.serialVersionUID;

  private transient String[] paths;

  public DiffSummary(String[] paths) {
    this.paths = paths;
  }

  public List<String> getPaths() {
    return Collections.unmodifiableList(Arrays.asList(paths));
  }

  private void writeObject(ObjectOutputStream output) throws IOException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    try (DeflaterOutputStream out = new DeflaterOutputStream(buf)) {
      writeString(out, Joiner.on('\n').join(paths));
    }
    writeBytes(output, buf.toByteArray());
  }

  private void readObject(ObjectInputStream input) throws IOException {
    ByteArrayInputStream buf = new ByteArrayInputStream(readBytes(input));
    try (InflaterInputStream in = new InflaterInputStream(buf)) {
      List<String> l = Splitter.on('\n').splitToList(readString(in));
      paths = l.toArray(new String[l.size()]);
    }
  }
}

@@ -712,21 +712,22 @@
 ion.read
-Bytes
+String
 ;%0Aimport
@@ -781,38 +781,40 @@
 rialization.read
-String
+VarInt32
 ;%0Aimport static 
@@ -869,21 +869,22 @@
 on.write
-Bytes
+String
 ;%0Aimport
@@ -951,170 +951,18 @@
 rite
-String;%0A%0Aimport com.google.common.base.Joiner;%0Aimport com.google.common.base.Splitter;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;
+VarInt32;%0A
 %0Aimp
@@ -1669,62 +1669,42 @@
     
-ByteArrayOutputStream buf = new ByteArrayOutputStream(
+writeVarInt32(output, paths.length
 );%0A 
@@ -1755,35 +1755,38 @@
 terOutputStream(
-buf
+output
 )) %7B%0A      write
@@ -1784,61 +1784,37 @@
     
-write
+for (
 String
-(out, Joiner.on('%5Cn').join(
+ p : 
 paths)
-);
+ %7B
 %0A    
-%7D%0A
     
@@ -1822,41 +1822,37 @@
 rite
-Bytes(output, buf.toByteArray());
+String(out, p);%0A      %7D%0A    %7D
 %0A  %7D
@@ -1933,77 +1933,47 @@
     
-ByteArrayInputStream buf = new ByteArrayInputStream(readBytes
+paths = new String%5BreadVarInt32
 (input)
-)
+%5D
 ;%0A  
@@ -2032,11 +2032,13 @@
 eam(
-buf
+input
 )) %7B
@@ -2048,118 +2048,91 @@
     
-List%3CString%3E l = Splitter.on('%5Cn').splitToList(readString(in));%0A      paths = l.toArray(new String%5Bl.size()%5D);
+for (int i = 0; i %3C paths.length; i++) %7B%0A        paths%5Bi%5D = readString(in);%0A      %7D
 %0A   

Improve serialization