Java Code Examples for com.squareup.okhttp.internal.Util#readFully()

The following examples show how to use com.squareup.okhttp.internal.Util#readFully() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Response.java    From cordova-amazon-fireos with Apache License 2.0 6 votes vote down vote up
public final byte[] bytes() throws IOException {
  long contentLength = contentLength();
  if (contentLength > Integer.MAX_VALUE) {
    throw new IOException("Cannot buffer entire body for content length: " + contentLength);
  }

  if (contentLength != -1) {
    byte[] content = new byte[(int) contentLength];
    InputStream in = byteStream();
    Util.readFully(in, content);
    if (in.read() != -1) throw new IOException("Content-Length and stream length disagree");
    return content;

  } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Util.copy(byteStream(), out);
    return out.toByteArray();
  }
}
 
Example 2
Source File: Response.java    From IoTgo_Android_App with MIT License 6 votes vote down vote up
public final byte[] bytes() throws IOException {
  long contentLength = contentLength();
  if (contentLength > Integer.MAX_VALUE) {
    throw new IOException("Cannot buffer entire body for content length: " + contentLength);
  }

  if (contentLength != -1) {
    byte[] content = new byte[(int) contentLength];
    InputStream in = byteStream();
    Util.readFully(in, content);
    if (in.read() != -1) throw new IOException("Content-Length and stream length disagree");
    return content;

  } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Util.copy(byteStream(), out);
    return out.toByteArray();
  }
}
 
Example 3
Source File: Response.java    From wildfly-samples with MIT License 6 votes vote down vote up
public final byte[] bytes() throws IOException {
  long contentLength = contentLength();
  if (contentLength > Integer.MAX_VALUE) {
    throw new IOException("Cannot buffer entire body for content length: " + contentLength);
  }

  if (contentLength != -1) {
    byte[] content = new byte[(int) contentLength];
    InputStream in = byteStream();
    Util.readFully(in, content);
    if (in.read() != -1) throw new IOException("Content-Length and stream length disagree");
    return content;

  } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Util.copy(byteStream(), out);
    return out.toByteArray();
  }
}
 
Example 4
Source File: Response.java    From reader with MIT License 6 votes vote down vote up
public final byte[] bytes() throws IOException {
  long contentLength = contentLength();
  if (contentLength > Integer.MAX_VALUE) {
    throw new IOException("Cannot buffer entire body for content length: " + contentLength);
  }

  if (contentLength != -1) {
    byte[] content = new byte[(int) contentLength];
    InputStream in = byteStream();
    Util.readFully(in, content);
    if (in.read() != -1) throw new IOException("Content-Length and stream length disagree");
    return content;

  } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Util.copy(byteStream(), out);
    return out.toByteArray();
  }
}
 
Example 5
Source File: Response.java    From reader with MIT License 6 votes vote down vote up
public final byte[] bytes() throws IOException {
  long contentLength = contentLength();
  if (contentLength > Integer.MAX_VALUE) {
    throw new IOException("Cannot buffer entire body for content length: " + contentLength);
  }

  if (contentLength != -1) {
    byte[] content = new byte[(int) contentLength];
    InputStream in = byteStream();
    Util.readFully(in, content);
    if (in.read() != -1) throw new IOException("Content-Length and stream length disagree");
    return content;

  } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Util.copy(byteStream(), out);
    return out.toByteArray();
  }
}
 
Example 6
Source File: Response.java    From L.TileLayer.Cordova with MIT License 6 votes vote down vote up
public final byte[] bytes() throws IOException {
  long contentLength = contentLength();
  if (contentLength > Integer.MAX_VALUE) {
    throw new IOException("Cannot buffer entire body for content length: " + contentLength);
  }

  if (contentLength != -1) {
    byte[] content = new byte[(int) contentLength];
    InputStream in = byteStream();
    Util.readFully(in, content);
    if (in.read() != -1) throw new IOException("Content-Length and stream length disagree");
    return content;

  } else {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Util.copy(byteStream(), out);
    return out.toByteArray();
  }
}
 
Example 7
Source File: SpdyStream.java    From bluemix-parking-meter with MIT License 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
  assert (!Thread.holdsLock(SpdyStream.this));

  if (byteCount == 0) {
    return;
  }

  int pos;
  int limit;
  int firstNewByte;
  boolean finished;
  boolean flowControlError;
  synchronized (SpdyStream.this) {
    finished = this.finished;
    pos = this.pos;
    firstNewByte = this.limit;
    limit = this.limit;
    flowControlError = byteCount > buffer.length - available();
  }

  // If the peer sends more data than we can handle, discard it and close the connection.
  if (flowControlError) {
    Util.skipByReading(in, byteCount);
    closeLater(ErrorCode.FLOW_CONTROL_ERROR);
    return;
  }

  // Discard data received after the stream is finished. It's probably a benign race.
  if (finished) {
    Util.skipByReading(in, byteCount);
    return;
  }

  // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
  // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
  // writes will be blocked until reads complete.
  if (pos < limit) {
    int firstCopyCount = Math.min(byteCount, buffer.length - limit);
    Util.readFully(in, buffer, limit, firstCopyCount);
    limit += firstCopyCount;
    byteCount -= firstCopyCount;
    if (limit == buffer.length) {
      limit = 0;
    }
  }
  if (byteCount > 0) {
    Util.readFully(in, buffer, limit, byteCount);
    limit += byteCount;
  }

  synchronized (SpdyStream.this) {
    // Update the new limit, and mark the position as readable if necessary.
    this.limit = limit;
    if (this.pos == -1) {
      this.pos = firstNewByte;
      SpdyStream.this.notifyAll();
    }
  }
}
 
Example 8
Source File: SpdyStream.java    From cordova-amazon-fireos with Apache License 2.0 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
  assert (!Thread.holdsLock(SpdyStream.this));

  if (byteCount == 0) {
    return;
  }

  int pos;
  int limit;
  int firstNewByte;
  boolean finished;
  boolean flowControlError;
  synchronized (SpdyStream.this) {
    finished = this.finished;
    pos = this.pos;
    firstNewByte = this.limit;
    limit = this.limit;
    flowControlError = byteCount > buffer.length - available();
  }

  // If the peer sends more data than we can handle, discard it and close the connection.
  if (flowControlError) {
    Util.skipByReading(in, byteCount);
    closeLater(ErrorCode.FLOW_CONTROL_ERROR);
    return;
  }

  // Discard data received after the stream is finished. It's probably a benign race.
  if (finished) {
    Util.skipByReading(in, byteCount);
    return;
  }

  // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
  // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
  // writes will be blocked until reads complete.
  if (pos < limit) {
    int firstCopyCount = Math.min(byteCount, buffer.length - limit);
    Util.readFully(in, buffer, limit, firstCopyCount);
    limit += firstCopyCount;
    byteCount -= firstCopyCount;
    if (limit == buffer.length) {
      limit = 0;
    }
  }
  if (byteCount > 0) {
    Util.readFully(in, buffer, limit, byteCount);
    limit += byteCount;
  }

  synchronized (SpdyStream.this) {
    // Update the new limit, and mark the position as readable if necessary.
    this.limit = limit;
    if (this.pos == -1) {
      this.pos = firstNewByte;
      SpdyStream.this.notifyAll();
    }
  }
}
 
Example 9
Source File: SpdyStream.java    From cordova-android-chromeview with Apache License 2.0 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
  assert (!Thread.holdsLock(SpdyStream.this));

  if (byteCount == 0) {
    return;
  }

  int pos;
  int limit;
  int firstNewByte;
  boolean finished;
  boolean flowControlError;
  synchronized (SpdyStream.this) {
    finished = this.finished;
    pos = this.pos;
    firstNewByte = this.limit;
    limit = this.limit;
    flowControlError = byteCount > buffer.length - available();
  }

  // If the peer sends more data than we can handle, discard it and close the connection.
  if (flowControlError) {
    Util.skipByReading(in, byteCount);
    closeLater(SpdyStream.RST_FLOW_CONTROL_ERROR);
    return;
  }

  // Discard data received after the stream is finished. It's probably a benign race.
  if (finished) {
    Util.skipByReading(in, byteCount);
    return;
  }

  // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
  // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
  // writes will be blocked until reads complete.
  if (pos < limit) {
    int firstCopyCount = Math.min(byteCount, buffer.length - limit);
    Util.readFully(in, buffer, limit, firstCopyCount);
    limit += firstCopyCount;
    byteCount -= firstCopyCount;
    if (limit == buffer.length) {
      limit = 0;
    }
  }
  if (byteCount > 0) {
    Util.readFully(in, buffer, limit, byteCount);
    limit += byteCount;
  }

  synchronized (SpdyStream.this) {
    // Update the new limit, and mark the position as readable if necessary.
    this.limit = limit;
    if (this.pos == -1) {
      this.pos = firstNewByte;
      SpdyStream.this.notifyAll();
    }
  }
}
 
Example 10
Source File: SpdyStream.java    From phonegapbootcampsite with MIT License 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
  assert (!Thread.holdsLock(SpdyStream.this));

  if (byteCount == 0) {
    return;
  }

  int pos;
  int limit;
  int firstNewByte;
  boolean finished;
  boolean flowControlError;
  synchronized (SpdyStream.this) {
    finished = this.finished;
    pos = this.pos;
    firstNewByte = this.limit;
    limit = this.limit;
    flowControlError = byteCount > buffer.length - available();
  }

  // If the peer sends more data than we can handle, discard it and close the connection.
  if (flowControlError) {
    Util.skipByReading(in, byteCount);
    closeLater(ErrorCode.FLOW_CONTROL_ERROR);
    return;
  }

  // Discard data received after the stream is finished. It's probably a benign race.
  if (finished) {
    Util.skipByReading(in, byteCount);
    return;
  }

  // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
  // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
  // writes will be blocked until reads complete.
  if (pos < limit) {
    int firstCopyCount = Math.min(byteCount, buffer.length - limit);
    Util.readFully(in, buffer, limit, firstCopyCount);
    limit += firstCopyCount;
    byteCount -= firstCopyCount;
    if (limit == buffer.length) {
      limit = 0;
    }
  }
  if (byteCount > 0) {
    Util.readFully(in, buffer, limit, byteCount);
    limit += byteCount;
  }

  synchronized (SpdyStream.this) {
    // Update the new limit, and mark the position as readable if necessary.
    this.limit = limit;
    if (this.pos == -1) {
      this.pos = firstNewByte;
      SpdyStream.this.notifyAll();
    }
  }
}
 
Example 11
Source File: SpdyStream.java    From reader with MIT License 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
  assert (!Thread.holdsLock(SpdyStream.this));

  if (byteCount == 0) {
    return;
  }

  int pos;
  int limit;
  int firstNewByte;
  boolean finished;
  boolean flowControlError;
  synchronized (SpdyStream.this) {
    finished = this.finished;
    pos = this.pos;
    firstNewByte = this.limit;
    limit = this.limit;
    flowControlError = byteCount > buffer.length - available();
  }

  // If the peer sends more data than we can handle, discard it and close the connection.
  if (flowControlError) {
    Util.skipByReading(in, byteCount);
    closeLater(ErrorCode.FLOW_CONTROL_ERROR);
    return;
  }

  // Discard data received after the stream is finished. It's probably a benign race.
  if (finished) {
    Util.skipByReading(in, byteCount);
    return;
  }

  // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
  // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
  // writes will be blocked until reads complete.
  if (pos < limit) {
    int firstCopyCount = Math.min(byteCount, buffer.length - limit);
    Util.readFully(in, buffer, limit, firstCopyCount);
    limit += firstCopyCount;
    byteCount -= firstCopyCount;
    if (limit == buffer.length) {
      limit = 0;
    }
  }
  if (byteCount > 0) {
    Util.readFully(in, buffer, limit, byteCount);
    limit += byteCount;
  }

  synchronized (SpdyStream.this) {
    // Update the new limit, and mark the position as readable if necessary.
    this.limit = limit;
    if (this.pos == -1) {
      this.pos = firstNewByte;
      SpdyStream.this.notifyAll();
    }
  }
}
 
Example 12
Source File: NameValueBlockReader.java    From bluemix-parking-meter with MIT License 4 votes vote down vote up
private String readString() throws DataFormatException, IOException {
  int length = nameValueBlockIn.readInt();
  byte[] bytes = new byte[length];
  Util.readFully(nameValueBlockIn, bytes);
  return new String(bytes, 0, length, "UTF-8");
}
 
Example 13
Source File: SpdyReader.java    From cordova-android-chromeview with Apache License 2.0 4 votes vote down vote up
private String readString() throws DataFormatException, IOException {
  int length = nameValueBlockIn.readInt();
  byte[] bytes = new byte[length];
  Util.readFully(nameValueBlockIn, bytes);
  return new String(bytes, 0, length, "UTF-8");
}
 
Example 14
Source File: Spdy3.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
private String readString() throws DataFormatException, IOException {
    int length = nameValueBlockIn.readInt();
    byte[] bytes = new byte[length];
    Util.readFully(nameValueBlockIn, bytes);
    return new String(bytes, 0, length, "UTF-8");
}
 
Example 15
Source File: SpdyStream.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
    assert (!Thread.holdsLock(SpdyStream.this));

    if (byteCount == 0) {
        return;
    }

    int pos;
    int limit;
    int firstNewByte;
    boolean finished;
    boolean flowControlError;
    synchronized (SpdyStream.this) {
        finished = this.finished;
        pos = this.pos;
        firstNewByte = this.limit;
        limit = this.limit;
        flowControlError = byteCount > buffer.length - available();
    }

    // If the peer sends more data than we can handle, discard it and close the connection.
    if (flowControlError) {
        Util.skipByReading(in, byteCount);
        closeLater(ErrorCode.FLOW_CONTROL_ERROR);
        return;
    }

    // Discard data received after the stream is finished. It's probably a benign race.
    if (finished) {
        Util.skipByReading(in, byteCount);
        return;
    }

    // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
    // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
    // writes will be blocked until reads complete.
    if (pos < limit) {
        int firstCopyCount = Math.min(byteCount, buffer.length - limit);
        Util.readFully(in, buffer, limit, firstCopyCount);
        limit += firstCopyCount;
        byteCount -= firstCopyCount;
        if (limit == buffer.length) {
            limit = 0;
        }
    }
    if (byteCount > 0) {
        Util.readFully(in, buffer, limit, byteCount);
        limit += byteCount;
    }

    synchronized (SpdyStream.this) {
        // Update the new limit, and mark the position as readable if necessary.
        this.limit = limit;
        if (this.pos == -1) {
            this.pos = firstNewByte;
            SpdyStream.this.notifyAll();
        }
    }
}
 
Example 16
Source File: NameValueBlockReader.java    From crosswalk-cordova-android with Apache License 2.0 4 votes vote down vote up
private String readString() throws DataFormatException, IOException {
  int length = nameValueBlockIn.readInt();
  byte[] bytes = new byte[length];
  Util.readFully(nameValueBlockIn, bytes);
  return new String(bytes, 0, length, "UTF-8");
}
 
Example 17
Source File: NameValueBlockReader.java    From phonegapbootcampsite with MIT License 4 votes vote down vote up
private String readString() throws DataFormatException, IOException {
  int length = nameValueBlockIn.readInt();
  byte[] bytes = new byte[length];
  Util.readFully(nameValueBlockIn, bytes);
  return new String(bytes, 0, length, "UTF-8");
}
 
Example 18
Source File: SpdyStream.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
  assert (!Thread.holdsLock(SpdyStream.this));

  if (byteCount == 0) {
    return;
  }

  int pos;
  int limit;
  int firstNewByte;
  boolean finished;
  boolean flowControlError;
  synchronized (SpdyStream.this) {
    finished = this.finished;
    pos = this.pos;
    firstNewByte = this.limit;
    limit = this.limit;
    flowControlError = byteCount > buffer.length - available();
  }

  // If the peer sends more data than we can handle, discard it and close the connection.
  if (flowControlError) {
    Util.skipByReading(in, byteCount);
    closeLater(ErrorCode.FLOW_CONTROL_ERROR);
    return;
  }

  // Discard data received after the stream is finished. It's probably a benign race.
  if (finished) {
    Util.skipByReading(in, byteCount);
    return;
  }

  // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
  // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
  // writes will be blocked until reads complete.
  if (pos < limit) {
    int firstCopyCount = Math.min(byteCount, buffer.length - limit);
    Util.readFully(in, buffer, limit, firstCopyCount);
    limit += firstCopyCount;
    byteCount -= firstCopyCount;
    if (limit == buffer.length) {
      limit = 0;
    }
  }
  if (byteCount > 0) {
    Util.readFully(in, buffer, limit, byteCount);
    limit += byteCount;
  }

  synchronized (SpdyStream.this) {
    // Update the new limit, and mark the position as readable if necessary.
    this.limit = limit;
    if (this.pos == -1) {
      this.pos = firstNewByte;
      SpdyStream.this.notifyAll();
    }
  }
}
 
Example 19
Source File: NameValueBlockReader.java    From L.TileLayer.Cordova with MIT License 4 votes vote down vote up
private String readString() throws DataFormatException, IOException {
  int length = nameValueBlockIn.readInt();
  byte[] bytes = new byte[length];
  Util.readFully(nameValueBlockIn, bytes);
  return new String(bytes, 0, length, "UTF-8");
}
 
Example 20
Source File: SpdyStream.java    From L.TileLayer.Cordova with MIT License 4 votes vote down vote up
void receive(InputStream in, int byteCount) throws IOException {
  assert (!Thread.holdsLock(SpdyStream.this));

  if (byteCount == 0) {
    return;
  }

  int pos;
  int limit;
  int firstNewByte;
  boolean finished;
  boolean flowControlError;
  synchronized (SpdyStream.this) {
    finished = this.finished;
    pos = this.pos;
    firstNewByte = this.limit;
    limit = this.limit;
    flowControlError = byteCount > buffer.length - available();
  }

  // If the peer sends more data than we can handle, discard it and close the connection.
  if (flowControlError) {
    Util.skipByReading(in, byteCount);
    closeLater(ErrorCode.FLOW_CONTROL_ERROR);
    return;
  }

  // Discard data received after the stream is finished. It's probably a benign race.
  if (finished) {
    Util.skipByReading(in, byteCount);
    return;
  }

  // Fill the buffer without holding any locks. First fill [limit..buffer.length) if that
  // won't overwrite unread data. Then fill [limit..pos). We can't hold a lock, otherwise
  // writes will be blocked until reads complete.
  if (pos < limit) {
    int firstCopyCount = Math.min(byteCount, buffer.length - limit);
    Util.readFully(in, buffer, limit, firstCopyCount);
    limit += firstCopyCount;
    byteCount -= firstCopyCount;
    if (limit == buffer.length) {
      limit = 0;
    }
  }
  if (byteCount > 0) {
    Util.readFully(in, buffer, limit, byteCount);
    limit += byteCount;
  }

  synchronized (SpdyStream.this) {
    // Update the new limit, and mark the position as readable if necessary.
    this.limit = limit;
    if (this.pos == -1) {
      this.pos = firstNewByte;
      SpdyStream.this.notifyAll();
    }
  }
}