Copyright (c) 2015, Chris Greenhalgh
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met...
If you think the Android project wototoplayer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2010 The Android Open Source Project
*/*fromwww.java2s.com*/
* 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.squareup.okhttp.internal.http;
import com.squareup.okhttp.internal.AbstractOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ProtocolException;
importstatic com.squareup.okhttp.internal.Util.checkOffsetAndCount;
/**
* An HTTP request body that's completely buffered in memory. This allows
* the post body to be transparently re-sent if the HTTP request must be
* sent multiple times.
*/finalclass RetryableOutputStream extends AbstractOutputStream {
privatefinalint limit;
privatefinal ByteArrayOutputStream content;
public RetryableOutputStream(int limit) {
this.limit = limit;
this.content = new ByteArrayOutputStream(limit);
}
public RetryableOutputStream() {
this.limit = -1;
this.content = new ByteArrayOutputStream();
}
@Override publicsynchronizedvoid close() throws IOException {
if (closed) {
return;
}
closed = true;
if (content.size() < limit) {
thrownew ProtocolException(
"content-length promised " + limit + " bytes, but received " + content.size());
}
}
@Override publicsynchronizedvoid write(byte[] buffer, int offset, int count)
throws IOException {
checkNotClosed();
checkOffsetAndCount(buffer.length, offset, count);
if (limit != -1 && content.size() > limit - count) {
thrownew ProtocolException("exceeded content-length limit of " + limit + " bytes");
}
content.write(buffer, offset, count);
}
publicsynchronizedint contentLength() throws IOException {
close();
return content.size();
}
publicvoid writeToSocket(OutputStream socketOut) throws IOException {
content.writeTo(socketOut);
}
}