aboutsummaryrefslogtreecommitdiff
path: root/gerrit-server/src/main/java/com/google/gerrit/server/patch/Text.java
blob: b0fa94787e51277d8ac33ce1883b7891b4512bad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (C) 2009 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 org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;
import org.mozilla.universalchardet.UniversalDetector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

public class Text extends RawText {
  private static final Logger log = LoggerFactory.getLogger(Text.class);
  private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
  private static final int bigFileThreshold = 10 * 1024 * 1024;

  public static final byte[] NO_BYTES = {};
  public static final Text EMPTY = new Text(NO_BYTES);

  public static String asString(byte[] content, String encoding) {
    return new String(content, charset(content, encoding));
  }

  public static byte[] asByteArray(ObjectLoader ldr)
      throws MissingObjectException, LargeObjectException, IOException {
    if (!ldr.isLarge()) {
      return ldr.getCachedBytes();
    }

    long sz = ldr.getSize();
    if (sz > bigFileThreshold || sz > Integer.MAX_VALUE)
      throw new LargeObjectException();

    byte[] buf;
    try {
      buf = new byte[(int) sz];
    } catch (OutOfMemoryError noMemory) {
      LargeObjectException e;

      e = new LargeObjectException();
      e.initCause(noMemory);
      throw e;
    }

    InputStream in = ldr.openStream();
    try {
      IO.readFully(in, buf, 0, buf.length);
    } finally {
      in.close();
    }
    return buf;
  }

  private static Charset charset(byte[] content, String encoding) {
    if (encoding == null) {
      UniversalDetector d = new UniversalDetector(null);
      d.handleData(content, 0, content.length);
      d.dataEnd();
      encoding = d.getDetectedCharset();
    }
    if (encoding == null) {
      return ISO_8859_1;
    }
    try {
      return Charset.forName(encoding);

    } catch (IllegalCharsetNameException err) {
      log.error("Invalid detected charset name '" + encoding + "': " + err);
      return ISO_8859_1;

    } catch (UnsupportedCharsetException err) {
      log.error("Detected charset '" + encoding + "' not supported: " + err);
      return ISO_8859_1;
    }
  }

  private Charset charset;

  public Text(final byte[] r) {
    super(r);
  }

  public Text(ObjectLoader ldr) throws MissingObjectException,
      LargeObjectException, IOException {
    this(asByteArray(ldr));
  }

  public byte[] getContent() {
    return content;
  }

  public String getLine(final int i) {
    return getLines(i, i + 1, true);
  }

  public String getLines(final int begin, final int end, boolean dropLF) {
    if (begin == end) {
      return "";
    }

    final int s = getLineStart(begin);
    int e = getLineEnd(end - 1);
    if (dropLF && content[e - 1] == '\n') {
      e--;
    }
    return decode(s, e);
  }

  private String decode(final int s, int e) {
    if (charset == null) {
      charset = charset(content, null);
    }
    return RawParseUtils.decode(charset, content, s, e);
  }

  private int getLineStart(final int i) {
    return lines.get(i + 1);
  }

  private int getLineEnd(final int i) {
    return lines.get(i + 2);
  }
}