LCOV - code coverage report
Current view: top level - common - RawInputUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 11 13 84.6 %
Date: 2022-11-19 15:00:39 Functions: 11 13 84.6 %

          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.common;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkArgument;
      18             : import static java.nio.charset.StandardCharsets.UTF_8;
      19             : import static java.util.Objects.requireNonNull;
      20             : 
      21             : import com.google.gerrit.extensions.restapi.RawInput;
      22             : import java.io.ByteArrayInputStream;
      23             : import java.io.IOException;
      24             : import java.io.InputStream;
      25             : import javax.servlet.http.HttpServletRequest;
      26             : 
      27           0 : public class RawInputUtil {
      28             :   public static RawInput create(String content) {
      29          25 :     return create(content.getBytes(UTF_8));
      30             :   }
      31             : 
      32             :   public static RawInput create(byte[] bytes, String contentType) {
      33          32 :     requireNonNull(bytes);
      34          32 :     checkArgument(bytes.length > 0);
      35          32 :     return new RawInput() {
      36             :       @Override
      37             :       public InputStream getInputStream() throws IOException {
      38          31 :         return new ByteArrayInputStream(bytes);
      39             :       }
      40             : 
      41             :       @Override
      42             :       public String getContentType() {
      43           1 :         return contentType;
      44             :       }
      45             : 
      46             :       @Override
      47             :       public long getContentLength() {
      48          26 :         return bytes.length;
      49             :       }
      50             :     };
      51             :   }
      52             : 
      53             :   public static RawInput create(byte[] bytes) {
      54          32 :     return create(bytes, "application/octet-stream");
      55             :   }
      56             : 
      57             :   public static RawInput create(HttpServletRequest req) {
      58           2 :     return new RawInput() {
      59             :       @Override
      60             :       public String getContentType() {
      61           0 :         return req.getContentType();
      62             :       }
      63             : 
      64             :       @Override
      65             :       public long getContentLength() {
      66           2 :         return req.getContentLength();
      67             :       }
      68             : 
      69             :       @Override
      70             :       public InputStream getInputStream() throws IOException {
      71           2 :         return req.getInputStream();
      72             :       }
      73             :     };
      74             :   }
      75             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750