final class Relay
extends java.lang.Object
As bytes are returned from upstream they are written to a local file. Downstream sources read from this file as necessary.
This class also keeps a small buffer of bytes recently read from upstream. This is intended to save a small amount of file I/O and data copying.
Modifier and Type | Class and Description |
---|---|
(package private) class |
Relay.RelaySource |
Modifier and Type | Field and Description |
---|---|
(package private) okio.Buffer |
buffer
The most recently read bytes from
upstream . |
(package private) long |
bufferMaxSize
The maximum size of
buffer . |
(package private) boolean |
complete
True if there are no further bytes to read from
upstream . |
(package private) java.io.RandomAccessFile |
file
Read/write persistence of the upstream source and its metadata.
|
private static long |
FILE_HEADER_SIZE |
private okio.ByteString |
metadata
User-supplied additional data persisted with the source data.
|
(package private) static okio.ByteString |
PREFIX_CLEAN |
(package private) static okio.ByteString |
PREFIX_DIRTY |
private static int |
SOURCE_FILE |
private static int |
SOURCE_UPSTREAM |
(package private) int |
sourceCount
Reference count of the number of active sources reading this stream.
|
(package private) okio.Source |
upstream
Null once the file has a complete copy of the upstream bytes.
|
(package private) okio.Buffer |
upstreamBuffer
A buffer for
upstreamReader to use when pulling bytes from upstream. |
(package private) long |
upstreamPos
The number of bytes consumed from
upstream . |
(package private) java.lang.Thread |
upstreamReader
The thread that currently has access to upstream.
|
Modifier | Constructor and Description |
---|---|
private |
Relay(java.io.RandomAccessFile file,
okio.Source upstream,
long upstreamPos,
okio.ByteString metadata,
long bufferMaxSize) |
Modifier and Type | Method and Description |
---|---|
(package private) void |
commit(long upstreamSize) |
static Relay |
edit(java.io.File file,
okio.Source upstream,
okio.ByteString metadata,
long bufferMaxSize)
Creates a new relay that reads a live stream from
upstream , using file to share
that data with other sources. |
(package private) boolean |
isClosed() |
okio.ByteString |
metadata() |
okio.Source |
newSource()
Returns a new source that returns the same bytes as upstream.
|
static Relay |
read(java.io.File file)
Creates a relay that reads a recorded stream from
file . |
private void |
writeHeader(okio.ByteString prefix,
long upstreamSize,
long metadataSize) |
private void |
writeMetadata(long upstreamSize) |
private static final int SOURCE_UPSTREAM
private static final int SOURCE_FILE
static final okio.ByteString PREFIX_CLEAN
static final okio.ByteString PREFIX_DIRTY
private static final long FILE_HEADER_SIZE
java.io.RandomAccessFile file
OkHttp cache v1\n
if the persisted file is complete. This is
another sequence of bytes if the file is incomplete and should not be used.
This is closed and assigned to null when the last source is closed and no further sources are permitted.
java.lang.Thread upstreamReader
okio.Source upstream
upstreamReader
thread may access this source.final okio.Buffer upstreamBuffer
upstreamReader
to use when pulling bytes from upstream. Only the upstreamReader
thread may access this buffer.long upstreamPos
upstream
. Guarded by this.boolean complete
upstream
. Guarded by this.private final okio.ByteString metadata
final okio.Buffer buffer
final long bufferMaxSize
buffer
.int sourceCount
newSource()
return null. Guarded by
this.private Relay(java.io.RandomAccessFile file, okio.Source upstream, long upstreamPos, okio.ByteString metadata, long bufferMaxSize)
public static Relay edit(java.io.File file, okio.Source upstream, okio.ByteString metadata, long bufferMaxSize) throws java.io.IOException
upstream
, using file
to share
that data with other sources.
Warning: callers to this method must immediately call newSource()
to
create a source and close that when they're done. Otherwise a handle to file
will be
leaked.
java.io.IOException
public static Relay read(java.io.File file) throws java.io.IOException
file
.
Warning: callers to this method must immediately call newSource()
to
create a source and close that when they're done. Otherwise a handle to file
will be
leaked.
java.io.IOException
private void writeHeader(okio.ByteString prefix, long upstreamSize, long metadataSize) throws java.io.IOException
java.io.IOException
private void writeMetadata(long upstreamSize) throws java.io.IOException
java.io.IOException
void commit(long upstreamSize) throws java.io.IOException
java.io.IOException
boolean isClosed()
public okio.ByteString metadata()
public okio.Source newSource()
read(java.io.File)
.