/*
 * Copyright (c) 2010 Google Inc.
 *
 * 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.api.client.http.javanet;

import com.google.api.client.http.LowLevelHttpResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

final class NetHttpResponse extends LowLevelHttpResponse {

  private final HttpURLConnection connection;
  private final int responseCode;
  private final String responseMessage;
  private final ArrayList<String> headerNames = new ArrayList<String>();
  private final ArrayList<String> headerValues = new ArrayList<String>();

  NetHttpResponse(HttpURLConnection connection) throws IOException {
    this.connection = connection;
    int responseCode = connection.getResponseCode();
    this.responseCode = responseCode == -1 ? 0 : responseCode;
    responseMessage = connection.getResponseMessage();
    List<String> headerNames = this.headerNames;
    List<String> headerValues = this.headerValues;
    for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) {
      String key = entry.getKey();
      if (key != null) {
        for (String value : entry.getValue()) {
          if (value != null) {
            headerNames.add(key);
            headerValues.add(value);
          }
        }
      }
    }
  }

  @Override
  public int getStatusCode() {
    return responseCode;
  }

  /**
   * {@inheritDoc}
   *
   * <p>
   * Returns {@link HttpURLConnection#getInputStream} when it doesn't throw {@link IOException},
   * otherwise it returns {@link HttpURLConnection#getErrorStream}.
   * </p>
   *
   * <p>
   * Upgrade warning: in prior version 1.16 {@link #getContent()} returned
   * {@link HttpURLConnection#getInputStream} only when the status code was successful. Starting
   * with version 1.17 it returns {@link HttpURLConnection#getInputStream} when it doesn't throw
   * {@link IOException}, otherwise it returns {@link HttpURLConnection#getErrorStream}
   * </p>
   */
  @Override
  public InputStream getContent() throws IOException {
    try {
      return connection.getInputStream();
    } catch (IOException ioe) {
      InputStream stream = connection.getErrorStream();
      // if the stream is not null then the error was set on the error stream, otherwise we should
      // just rethrow the I/O exception.
      if (stream == null) {
        throw ioe;
      }
      return stream;
    }
  }

  @Override
  public String getContentEncoding() {
    return connection.getContentEncoding();
  }

  @Override
  public long getContentLength() {
    String string = connection.getHeaderField("Content-Length");
    return string == null ? -1 : Long.parseLong(string);
  }

  @Override
  public String getContentType() {
    return connection.getHeaderField("Content-Type");
  }

  @Override
  public String getReasonPhrase() {
    return responseMessage;
  }

  @Override
  public String getStatusLine() {
    String result = connection.getHeaderField(0);
    return result != null && result.startsWith("HTTP/1.") ? result : null;
  }

  @Override
  public int getHeaderCount() {
    return headerNames.size();
  }

  @Override
  public String getHeaderName(int index) {
    return headerNames.get(index);
  }

  @Override
  public String getHeaderValue(int index) {
    return headerValues.get(index);
  }

  /**
   * Closes the connection to the HTTP server.
   *
   * @since 1.4
   */
  @Override
  public void disconnect() {
    connection.disconnect();
  }
}

@@ -2730,28 +2730,14 @@
     
-InputStream stream =
+return
  con
@@ -2766,224 +2766,8 @@
 ();%0A
-      // if the stream is not null then the error was set on the error stream, otherwise we should%0A      // just rethrow the I/O exception.%0A      if (stream == null) %7B%0A        throw ioe;%0A      %7D%0A      return stream;%0A
     

Fix a bug in NetHttpResponse (don't catch IOException when getErrorStream)