Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Few 'Ok' Libraries (Droidcon MTL 2015)

A Few 'Ok' Libraries (Droidcon MTL 2015)

This talk will be an in-depth look at Okio—a tiny library for interacting with bytes—and a few of the libraries written on top of it: OkHttp, Retrofit, and a newcomer named Moshi.

Okio evolved naturally inside of OkHttp before being split out into its own library. It wraps common patterns behind a friendly API for reading, writing, and processing data. We'll start with some fundamentals of the library and how it can enable you to work very close to raw data with ease.

After an introduction to Okio, we will look at three libraries written with it: OkHttp, a modern HTTP client; Retrofit, a high-level HTTP wrapper; and Moshi, a brand new library for serialization.

Not only are these libraries powerful on their own, but when combined their efficiency and performance dramatically increase. We'll conclude with demonstration of how to use them in your applications to great effect.

Video: https://youtu.be/WvyScM_S88c

Jake Wharton
PRO

April 09, 2015
Tweet

More Decks by Jake Wharton

Other Decks in Technology

Transcript

  1. A Few “OK” Libraries
    Jake Wharton

    View Slide

  2. okio

    View Slide

  3. okio

    View Slide

  4. okio

    View Slide

  5. okio
    okhttp
    moshi
    retrofit

    View Slide

  6. okio?

    View Slide

  7. HTTP

    View Slide

  8. HTTP
    GET  /a-­‐few-­‐ok-­‐libraries.html  HTTP/1.1  
    Host:  droidcon.ca

    View Slide

  9. HTTP
    GET  /a-­‐few-­‐ok-­‐libraries.html  HTTP/1.1  
    Host:  droidcon.ca
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    ...

    View Slide

  10. HTTP
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    ...

    View Slide

  11. HTTP
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    ...
                   ·∙      ·∙    \r\n  
             ·∙        ·∙  ·∙      ·∙        ·∙                ·∙      \r\n  
                 ·∙                                  ·∙              \r\n  
                             ·∙                                                  ·∙                          \r\n  
                                 ·∙        \r\n  
    \r\n

    View Slide

  12. HTTP
    HTTP/1.1·∙200·∙OK\r\n  
    Date:·∙Thu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMT\r\n  
    Server:·∙JakeWharton/2.3.7·∙(Human)\r\n  
    Content-­‐Type:·∙application/presentation;·∙charset=UTF-­‐8\r\n  
    Content-­‐Length:·∙3000\r\n  
    \r\n  
    ...

    View Slide

  13. HTTP
    HTTP/1.1·∙200·∙OK\r\nDate:·∙T  
    hu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMT\r\nServer:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...

    View Slide

  14. HTTP
    HTTP/1.1·∙200·∙OK\r\nDate:·∙T  
    hu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMT\r\nServer:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...

    View Slide

  15. HTTP
    HTTP/1.1·∙200·∙OK\r\nDate:·∙T  
    hu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMT\r\nServer:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...

    View Slide

  16. HTTP
                                         Date:·∙T  
    hu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMT\r\nServer:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    HTTP/1.1      200      OK

    View Slide

  17. HTTP
                                         Date:·∙T  
    hu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMT\r\nServer:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    HTTP/1.1      200      OK

    View Slide

  18. HTTP
                                         Date:·∙T  
    hu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMT\r\nServer:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    HTTP/1.1      200      OK

    View Slide

  19. HTTP
    !
                                                                 Server:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    HTTP/1.1      200      OK
    Date      Thu,  9  Apr  2015  15:14:34  GMT

    View Slide

  20. java.io.*

    View Slide

  21. java.io.*
    abstract class InputStream {

    }X

    View Slide

  22. java.io.*
    abstract class InputStream {

    void close();

    }X

    View Slide

  23. java.io.*
    abstract class InputStream {

    void close();

    long skip(long byteCount);

    }X

    View Slide

  24. java.io.*
    abstract class InputStream {

    void close();

    long skip(long byteCount);

    }X

    View Slide

  25. java.io.*
    abstract class InputStream {

    void close();

    long skip(long byteCount);

    }X

    while (byteCount > 0) {

    byteCount -= is.skip(byteCount);

    }X


    View Slide

  26. java.io.*
    abstract class InputStream {

    void close();

    long skip(long byteCount);

    }X
    public static void reallySkip(InputStream is, long byteCount) {

    while (byteCount > 0) {

    byteCount -= is.skip(byteCount);

    }X

    }

    View Slide

  27. java.io.*
    abstract class InputStream {

    void close();

    int read(byte[] buffer);

    int read(byte[] buffer, int offset, int count);

    long skip(long byteCount);

    }X

    View Slide

  28. java.io.*
    abstract class InputStream {

    void close();
    int read();

    int read(byte[] buffer);

    int read(byte[] buffer, int offset, int count);

    long skip(long byteCount);

    }X

    View Slide

  29. java.io.*
    abstract class InputStream {

    int available();

    void close();

    void mark(int readLimit);

    boolean markSupported();
    int read();

    int read(byte[] buffer);

    int read(byte[] buffer, int offset, int count);

    synchronized void reset();

    long skip(long byteCount);

    }X

    View Slide

  30. java.io.*
    • API is conflated with multiple concepts and inconsistent behavior

    View Slide

  31. HTTP
    !
                                                                 Server:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    HTTP/1.1      200      OK
    Date      Thu,  9  Apr  2015  15:14:34  GMT

    View Slide

  32. HTTP
    !
                                                                 Server:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    HTTP/1.1·∙200·∙OKrnDate:·∙Thu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMTrn

    View Slide

  33. HTTP
    !
                                                                 Server:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    HTTP/1.1·∙200·∙OKrnDate:·∙Thu,·∙9·∙Apr·∙2015·∙15:14:34·∙GMTrn

    View Slide

  34. HTTP
    !
                                                                 Server:·∙JakeWharto  
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...

    View Slide

  35. HTTP
    !
    !
    n/2.3.7·∙(Huma  
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharto

    View Slide

  36. HTTP
    !
    !
    !
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Huma

    View Slide

  37. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙app

    View Slide

  38. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙app

    View Slide

  39. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
                                                                         Content-­‐Type:·∙app
    Server      JakeWharton/2.3.7  (Human)

    View Slide

  40. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
                                                                         Content-­‐Type:·∙app

    View Slide

  41. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
                                                                         Content-­‐Type:·∙app
                                                                         Content-­‐Type:·∙app

    View Slide

  42. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
                                                                         Content-­‐Type:·∙app
    Content-­‐Type:·∙app

    View Slide

  43. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Content-­‐Type:·∙app

    View Slide

  44. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Content-­‐Type:·∙app

    View Slide

  45. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...

    View Slide

  46. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
                                                                         Content-­‐Type:·∙app

    View Slide

  47. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
                                                                         Content-­‐Type:·∙app

    View Slide

  48. HTTP
    !
    !
    !
                                                 lication/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
                                                                         Content-­‐Type:·∙app

    View Slide

  49. HTTP
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    lication/presentation;·∙charset          Content-­‐Type:·∙app

    View Slide

  50. HTTP
    !
    !
    !
    !
             8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    lication/presentation;·∙charset=UTF-­‐Content-­‐Type:·∙app

    View Slide

  51. java.io.*
    • API is conflated with multiple concepts and inconsistent behavior

    View Slide

  52. java.io.*
    • API is conflated with multiple concepts and inconsistent behavior
    !
    • Burden of figuring out storage is on the consumer

    View Slide

  53. java.io.*
    class BufferedInputStream extends InputStream {

    BufferedInputStream(InputStream is) {

    // ...

    }


    BufferedInputStream(InputStream is, int size) {

    // ...

    }

    }

    View Slide

  54. java.io.*
    !
    !
    !
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Huma

    View Slide

  55. java.io.*
    !
    !
    !
    n)\r\nContent-­‐Type:·∙application/presentation;·∙charset  
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Huma
    Server:·∙JakeWharton/2.3.7·∙(Huma

    View Slide

  56. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
    Server:·∙JakeWharton/2.3.7·∙(Huma

    View Slide

  57. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
    Server:·∙JakeWharton/2.3.7·∙(Huma
                                                                 n)rnContent-­‐Type:·∙app

    View Slide

  58. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙app

    View Slide

  59. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙app

    View Slide

  60. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
                                                                         Content-­‐Type:·∙app

    View Slide

  61. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
                                                                         Content-­‐Type:·∙app
                                                                                                           lication/presentation;·∙charset

    View Slide

  62. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
    lication/presentation;·∙charset          Content-­‐Type:·∙app

    View Slide

  63. java.io.*
    !
    !
    !
    !
    =UTF-­‐8\r\nContent-­‐Length:·∙3000\r  
    \n\r\n...
    Server:·∙JakeWharton/2.3.7·∙(Human)rnContent-­‐Type:·∙application/presentation;·∙charset
    lication/presentation;·∙charset          Content-­‐Type:·∙app

    View Slide

  64. java.io.*
    • API is conflated with multiple concepts and inconsistent behavior
    • Burden of figuring out storage is on the consumer

    View Slide

  65. java.io.*
    • API is conflated with multiple concepts and inconsistent behavior
    • Burden of figuring out storage is on the consumer
    !
    !
    • Decorators add performance, functionality

    View Slide

  66. java.io.*
    class DataInputStream extends InputStream {

    DataInputStream(InputStream in) {}


    void readFully(byte[] b) {}

    void readFully(byte b[], int off, int len) {}

    boolean readBoolean() {}

    byte readByte() {}

    int readUnsignedByte() {}

    short readShort() {}

    int readUnsignedShort() {}

    char readChar() {}

    int readInt() {}

    long readLong() {}

    float readFloat() {}

    double readDouble() {}

    String readLine() {}

    String readUTF() {}

    }

    View Slide

  67. java.io.*
    class DataInputStream extends InputStream {

    DataInputStream(InputStream in) {}


    void readFully(byte[] b) {}

    void readFully(byte b[], int off, int len) {}

    boolean readBoolean() {}

    byte readByte() {}

    int readUnsignedByte() {}

    short readShort() {}

    int readUnsignedShort() {}

    char readChar() {}

    int readInt() {}

    long readLong() {}

    float readFloat() {}

    double readDouble() {}

    String readLine() {}

    String readUTF() {}

    }
    class InputStreamReader extends Reader {

    InputStreamReader(InputStream is) {}

    InputStreamReader(InputStream is, String charsetName) {}

    InputStreamReader(InputStream is, CharsetDecoder dec) {}

    InputStreamReader(InputStream is, Charset charset) {}


    int read() {}

    int read(char[] buffer) {}

    int read(char[] buffer, int offset, int count) {}

    int read(CharBuffer buffer) {}

    }

    View Slide

  68. java.io.*
    • API is conflated with multiple concepts and inconsistent behavior
    • Burden of figuring out storage is on the consumer
    • Decorators add performance, functionality

    View Slide

  69. java.io.*
    • API is conflated with multiple concepts and inconsistent behavior
    • Burden of figuring out storage is on the consumer
    • Decorators add performance, functionality
    !
    !
    !
    • High-level abstractions exist, but do not compose

    View Slide

  70. Okio

    View Slide

  71. Okio
    • Complements java.io.* and java.nio.*

    View Slide

  72. Okio
    • Complements java.io.* and java.nio.*
    • Source and Sink move data

    View Slide

  73. Okio
    interface Source {
    }X

    View Slide

  74. Okio
    interface Source {

    void close();
    }X

    View Slide

  75. Okio
    interface Source {

    Timeout timeout();

    void close();
    }X

    View Slide

  76. Okio
    interface Source {

    long read(Buffer sink, long byteCount);

    Timeout timeout();

    void close();
    }X

    View Slide

  77. Okio
    interface Source {

    long read(Buffer sink, long byteCount);

    Timeout timeout();

    void close();
    }X

    View Slide

  78. Okio
    interface Sink {
    }X

    View Slide

  79. Okio
    interface Sink {

    void write(Buffer sink, long byteCount);

    Timeout timeout();

    void close();
    }X

    View Slide

  80. Okio
    interface Sink {

    void write(Buffer sink, long byteCount);

    Timeout timeout();

    void close();
    void flush();
    }X

    View Slide

  81. Okio
    interface Sink {

    void write(Buffer sink, long byteCount);

    Timeout timeout();

    void close();
    void flush();
    }X

    View Slide

  82. Okio
    • Complements java.io.* and java.nio.*  
    • Source and Sink move data

    View Slide

  83. Okio
    • Complements java.io.* and java.nio.*  
    • Source and Sink move data
    • Complements java.io.* and java.nio  *
    • Source and Sink move data
    • Buffer and ByteString hold data

    View Slide

  84. Okio
    Buffer buffer = new Buffer();

    View Slide

  85. Okio
    Buffer buffer = new Buffer();
    Buffer

    View Slide

  86. Okio
    Buffer buffer = new Buffer(); Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    Buffer

    View Slide

  87. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    Buffer

    View Slide

  88. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=0
    Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  89. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=15
    Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  90. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=15
    Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  91. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=30
    Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  92. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=30
    Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  93. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=30
    byte[]
    !
    pos=0
    limit=0
    Segment Pool
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  94. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=30
    Segment Pool
    byte[]
    !
    pos=0
    limit=15
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  95. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=30
    Segment Pool
    byte[]
    !
    pos=0
    limit=15
    byte[]
    !
    pos=0
    limit=0
    byte[]
    !
    pos=0
    limit=0

    View Slide

  96. Buffer
    Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    byte[]
    !
    pos=0
    limit=30
    byte[]
    !
    pos=0
    limit=15
    Segment Pool

    View Slide

  97. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    Buffer
    byte[]
    !
    pos=0
    limit=30
    byte[]
    !
    pos=0
    limit=15
    Segment Pool

    View Slide

  98. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    Buffer
    byte[]
    !
    pos=5
    limit=30
    byte[]
    !
    pos=0
    limit=15
    Segment Pool

    View Slide

  99. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    buffer.readUtf8(25);
    Buffer
    byte[]
    !
    pos=5
    limit=30
    byte[]
    !
    pos=0
    limit=15
    Segment Pool

    View Slide

  100. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    buffer.readUtf8(25);
    // ", DroidconHello, Droidcon"
    Buffer
    byte[]
    !
    pos=30
    limit=30
    byte[]
    !
    pos=0
    limit=15
    Segment Pool

    View Slide

  101. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    buffer.readUtf8(25);
    // ", DroidconHello, Droidcon"
    Buffer
    byte[]
    !
    pos=0
    limit=15
    Segment Pool

    View Slide

  102. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    buffer.readUtf8(25);
    // ", DroidconHello, Droidcon"
    !
    Buffer otherBuffer = new Buffer();
    Buffer
    byte[]
    !
    pos=0
    limit=15

    View Slide

  103. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    buffer.readUtf8(25);
    // ", DroidconHello, Droidcon"
    !
    Buffer otherBuffer = new Buffer();
    Buffer
    byte[]
    !
    pos=0
    limit=15
    Buffer

    View Slide

  104. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    buffer.readUtf8(25);
    // ", DroidconHello, Droidcon"
    !
    Buffer otherBuffer = new Buffer();
    otherBuffer.writeAll(buffer);
    Buffer
    byte[]
    !
    pos=0
    limit=15
    Buffer

    View Slide

  105. Okio
    Buffer buffer = new Buffer();
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon");
    buffer.writeUtf8("Hello, Droidcon”);
    !
    buffer.readUtf8(5);
    // "Hello"
    buffer.readUtf8(25);
    // ", DroidconHello, Droidcon"
    !
    Buffer otherBuffer = new Buffer();
    otherBuffer.writeAll(buffer);
    Buffer
    byte[]
    !
    pos=0
    limit=15
    Buffer

    View Slide

  106. Okio
    • Complements java.io.* and java.nio.*  
    • Source and Sink move data
    • Buffer and ByteString hold data

    View Slide

  107. Okio
    • Complements java.io.* and java.nio.*  
    • Source and Sink move data
    • Buffer and ByteString hold data
    • Complements java.io.* and java.nio.*
    • Source and Sink move data
    • Buffer and ByteString hold data
    • BufferedSource and BufferedSink move data efficiently

    View Slide

  108. Okio
    interface BufferedSink extends Sink {

    }X

    View Slide

  109. Okio
    interface BufferedSource extends Source {

    }X
    interface BufferedSink extends Sink {

    }X

    View Slide

  110. Okio
    interface BufferedSink extends Sink {

    BufferedSink write(ByteString byteString);

    BufferedSink write(byte[] source);

    BufferedSink write(byte[] source, int offset, int byteCount);

    long writeAll(Source source);

    BufferedSink write(Source source, long byteCount);

    BufferedSink writeUtf8(String string);

    BufferedSink writeString(String string, Charset charset);

    BufferedSink writeByte(int b);

    BufferedSink writeShort(int s);

    BufferedSink writeShortLe(int s);

    BufferedSink writeInt(int i);

    BufferedSink writeIntLe(int i);

    BufferedSink writeLong(long v);

    BufferedSink writeLongLe(long v);

    BufferedSink writeDecimalLong(long v);

    BufferedSink writeHexadecimalUnsignedLong(long v);

    }X
    interface BufferedSource extends Source {

    }X

    View Slide

  111. Okio
    interface BufferedSource extends Source {

    byte readByte();

    short readShort();

    short readShortLe();

    int readInt();

    int readIntLe();

    long readLong();

    long readLongLe();

    long readDecimalLong();

    long readHexadecimalUnsignedLong();

    ByteString readByteString();

    ByteString readByteString(long byteCount);

    byte[] readByteArray();

    byte[] readByteArray(long byteCount);

    int read(byte[] sink);

    void readFully(byte[] sink);

    int read(byte[] sink, int offset, int byteCount);

    void readFully(Buffer sink, long byteCount);

    long readAll(Sink sink);

    String readUtf8();

    String readUtf8(long byteCount);

    String readUtf8Line();

    String readUtf8LineStrict();

    String readString(Charset charset);

    String readString(long byteCount, Charset charset);

    long indexOf(byte b);

    long indexOf(byte b, long fromIndex);

    long indexOfElement(ByteString targetBytes);

    long indexOfElement(ByteString targetBytes, long fromIndex);

    }X
    interface BufferedSink extends Sink {

    BufferedSink write(ByteString byteString);

    BufferedSink write(byte[] source);

    BufferedSink write(byte[] source, int offset, int byteCount);

    long writeAll(Source source);

    BufferedSink write(Source source, long byteCount);

    BufferedSink writeUtf8(String string);

    BufferedSink writeString(String string, Charset charset);

    BufferedSink writeByte(int b);

    BufferedSink writeShort(int s);

    BufferedSink writeShortLe(int s);

    BufferedSink writeInt(int i);

    BufferedSink writeIntLe(int i);

    BufferedSink writeLong(long v);

    BufferedSink writeLongLe(long v);

    BufferedSink writeDecimalLong(long v);

    BufferedSink writeHexadecimalUnsignedLong(long v);

    }X

    View Slide

  112. Okio
    interface BufferedSource extends Source {

    byte readByte();

    short readShort();

    short readShortLe();

    int readInt();

    int readIntLe();

    long readLong();

    long readLongLe();

    long readDecimalLong();

    long readHexadecimalUnsignedLong();

    ByteString readByteString();

    ByteString readByteString(long byteCount);

    byte[] readByteArray();

    byte[] readByteArray(long byteCount);

    int read(byte[] sink);

    void readFully(byte[] sink);

    int read(byte[] sink, int offset, int byteCount);

    void readFully(Buffer sink, long byteCount);

    long readAll(Sink sink);

    String readUtf8();

    String readUtf8(long byteCount);

    String readUtf8Line();

    String readUtf8LineStrict();

    String readString(Charset charset);

    String readString(long byteCount, Charset charset);

    long indexOf(byte b);

    long indexOf(byte b, long fromIndex);

    long indexOfElement(ByteString targetBytes);

    long indexOfElement(ByteString targetBytes, long fromIndex);
    !
    boolean exhausted();

    void require(long byteCount);

    boolean request(long byteCount);

    void skip(long byteCount);
    !
    Buffer buffer();

    InputStream inputStream();

    }X
    interface BufferedSink extends Sink {

    BufferedSink write(ByteString byteString);

    BufferedSink write(byte[] source);

    BufferedSink write(byte[] source, int offset, int byteCount);

    long writeAll(Source source);

    BufferedSink write(Source source, long byteCount);

    BufferedSink writeUtf8(String string);

    BufferedSink writeString(String string, Charset charset);

    BufferedSink writeByte(int b);

    BufferedSink writeShort(int s);

    BufferedSink writeShortLe(int s);

    BufferedSink writeInt(int i);

    BufferedSink writeIntLe(int i);

    BufferedSink writeLong(long v);

    BufferedSink writeLongLe(long v);

    BufferedSink writeDecimalLong(long v);

    BufferedSink writeHexadecimalUnsignedLong(long v);

    BufferedSink emitCompleteSegments();
    BufferedSink emit();
    !
    Buffer buffer();

    OutputStream outputStream();

    }X

    View Slide

  113. Okio
    classxBuffer implements BufferedSink, BufferedSource {
    }

    View Slide

  114. Okio
    Socket

    View Slide

  115. Okio
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    Socket

    View Slide

  116. Okio
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket

    View Slide

  117. Okio
    BufferedSource Socket
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    source.readInt();

    View Slide

  118. Okio
    BufferedSource Socket
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    source.readInt();

    View Slide

  119. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt();

    View Slide

  120. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
           •••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    View Slide

  121. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
           •••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt();

    View Slide

  122. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
                   •••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    View Slide

  123. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
                   •••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4);

    View Slide

  124. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
                           •••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    View Slide

  125. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
                           •••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong();

    View Slide

  126. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
                             
             ••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    View Slide

  127. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
             ••••••••••

    View Slide

  128. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    View Slide

  129. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();

    View Slide

  130. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
    !
    !
    !
    !
                             ••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••

    View Slide

  131. Okio
    !
    !
    !
    !
    !
    !
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    •••••••••••••••
    BufferedSource Socket
    !
    !
    !
    !
                             ••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);

    View Slide

  132. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    !
    !
    !
    !
                             ••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);

    View Slide

  133. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    !
    !
    !
    !
                             ••  
    •••••••••••••••
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••

    View Slide

  134. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    !
                 ••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••

    View Slide

  135. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    !
                 ••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••

    View Slide

  136. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    !
                 ••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••

    View Slide

  137. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••

    View Slide

  138. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt();
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••

    View Slide

  139. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    !
    !
    !
    !
           •••••••••••  
    •••••••••••••••

    View Slide

  140. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt();
    !
    !
    !
    !
           •••••••••••  
    •••••••••••••••

    View Slide

  141. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    !
    !
    !
    !
                   •••••••  
    •••••••••••••••

    View Slide

  142. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    source.readLong();
    !
    !
    !
    !
                   •••••••  
    •••••••••••••••

    View Slide

  143. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    source.readLong(); ••••••••
    !
    !
    !
    !
                                   
     ••••••••••••••

    View Slide

  144. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    source.readLong(); ••••••••
    source.readLong();
    !
    !
    !
    !
                                   
     ••••••••••••••

    View Slide

  145. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    source.readLong(); ••••••••
    source.readLong(); ••••••••
    !
    !
    !
    !
                                   
                     ••••••

    View Slide

  146. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    source.readLong(); ••••••••
    source.readLong(); ••••••••
    source.readUtf8(6);
    !
    !
    !
    !
                                   
                     ••••••

    View Slide

  147. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    source.readLong(); ••••••••
    source.readLong(); ••••••••
    source.readUtf8(6); ••••••

    View Slide

  148. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    !
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••  
    •••••••••••••••
    BufferedSource Socket
    source.readInt(); ••••

    source.readInt(); ••••

    source.readUtf8(4); ••••

    source.readLong(); ••••••••

    source.readUtf8LineStrict();
    •••••••••••••••••••••••••

    source.readUtf8LineStrict();
    ••••••••••••••••••••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readUtf8(40);
    ••••••••••••••••••••••••••••
    •••••••••••
    source.readInt(); ••••
    source.readInt(); ••••
    source.readLong(); ••••••••
    source.readLong(); ••••••••
    source.readUtf8(6); ••••••

    View Slide

  149. Okio
    class Okio {

    static BufferedSource buffer(Source source) {}

    static BufferedSink buffer(Sink sink) {}

    }X

    View Slide

  150. Okio
    class Okio {

    static BufferedSource buffer(Source source) {}

    static BufferedSink buffer(Sink sink) {}


    static Source source(InputStream in) {}

    static Sink sink(OutputStream out) {}

    }X

    View Slide

  151. Okio
    class Okio {

    static BufferedSource buffer(Source source) {}

    static BufferedSink buffer(Sink sink) {}


    static Source source(InputStream in) {}

    static Sink sink(OutputStream out) {}


    static Source source(Socket socket) {}

    static Sink sink(Socket socket) {}

    }X

    View Slide

  152. Okio
    class Okio {

    static BufferedSource buffer(Source source) {}

    static BufferedSink buffer(Sink sink) {}


    static Source source(InputStream in) {}

    static Sink sink(OutputStream out) {}


    static Source source(Socket socket) {}

    static Sink sink(Socket socket) {}


    static Source source(File file) {}

    static Sink sink(File file) {}

    static Sink appendingSink(File file) {}

    }X

    View Slide

  153. Okio
    class Okio {

    static BufferedSource buffer(Source source) {}

    static BufferedSink buffer(Sink sink) {}


    static Source source(InputStream in) {}

    static Sink sink(OutputStream out) {}


    static Source source(Socket socket) {}

    static Sink sink(Socket socket) {}


    static Source source(File file) {}

    static Sink sink(File file) {}

    static Sink appendingSink(File file) {}


    static Source source(Path path, OpenOption... options) {}

    static Sink sink(Path path, OpenOption... options) {}

    }X

    View Slide

  154. Okio
    class DeflaterSink {

    DeflaterSink(Sink sink, Deflater deflater) {}

    }

    View Slide

  155. Okio
    !
    !
    !
    !
    class InflaterSource {

    InflaterSource(Source source, Inflater inflater) {}

    }
    class DeflaterSink {

    DeflaterSink(Sink sink, Deflater deflater) {}

    }

    View Slide

  156. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    class GzipSink {

    GzipSink(Sink sink) {}

    }
    !
    !
    !
    !
    class InflaterSource {

    InflaterSource(Source source, Inflater inflater) {}

    }
    class DeflaterSink {

    DeflaterSink(Sink sink, Deflater deflater) {}

    }

    View Slide

  157. Okio
    !
    !
    !
    !
    !
    !
    !
    !
    class GzipSink {

    GzipSink(Sink sink) {}

    }












    class GzipSource {

    GzipSource(Source source) {}

    }
    !
    !
    !
    !
    class InflaterSource {

    InflaterSource(Source source, Inflater inflater) {}

    }
    class DeflaterSink {

    DeflaterSink(Sink sink, Deflater deflater) {}

    }

    View Slide

  158. Okio
    File file = // ...
    File

    View Slide

  159. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);
    File
    FileSink

    View Slide

  160. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);
    File
    GzipSink FileSink

    View Slide

  161. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    BufferedSink bufferedSink = Okio.buffer(gzipSink);
    File
    BufferedSink GzipSink FileSink

    View Slide

  162. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    BufferedSink bufferedSink = Okio.buffer(gzipSink);

    bufferedSink.writeUtf8("Hello, hello, hello!");
    File
    FileSink
    GzipSink
    BufferedSink




    "Hello, hello, hello!"

    View Slide

  163. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    BufferedSink bufferedSink = Okio.buffer(gzipSink);

    bufferedSink.writeUtf8("Hello, hello, hello!");
    File
    FileSink
    GzipSink
    BufferedSink
    Hello, hello, hello!

    View Slide

  164. x??H????Q?@?H??
    Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    BufferedSink bufferedSink = Okio.buffer(gzipSink);

    bufferedSink.writeUtf8("Hello, hello, hello!");
    bufferedSink.close();
    File
    FileSink
    GzipSink
    BufferedSink
    Hello, hello, hello!

    View Slide

  165. Hello, hello, hello!
    Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    BufferedSink bufferedSink = Okio.buffer(gzipSink);

    bufferedSink.writeUtf8("Hello, hello, hello!");
    bufferedSink.close();
    File
    FileSink
    GzipSink
    BufferedSink
    x??H????Q?@?H??

    View Slide

  166. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    BufferedSink bufferedSink = Okio.buffer(gzipSink);

    bufferedSink.writeUtf8("Hello, hello, hello!");
    bufferedSink.close();
    File
    FileSink
    GzipSink
    BufferedSink
    x??H????Q?@?H??

    View Slide

  167. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    BufferedSink bufferedSink = Okio.buffer(gzipSink);

    bufferedSink.writeUtf8("Hello, hello, hello!");
    bufferedSink.close();
    File
    FileSink
    GzipSink
    BufferedSink
    x??H????Q?@?H??

    View Slide

  168. Okio
    File file = // ...

    Sink fileSink = Okio.sink(file);

    Sink gzipSink = new GzipSink(fileSink);

    Sha1Sink hashingSink = new Sha1Sink(gzipSink);

    BufferedSink bufferedSink = Okio.buffer(hashingSink);


    bufferedSink.writeUtf8("Hello, hello, hello!");

    bufferedSink.close();


    String hash = hashingSink.hash();
    File
    FileSink
    GzipSink
    BufferedSink Sha1Sink

    View Slide

  169. Okio
    Socket socket = //...

    Source socketSource = Okio.source(socket);

    View Slide

  170. Okio
    Socket socket = //...

    Source socketSource = Okio.source(socket);


    Throttler throttler = new Throttler();

    Source throttledSource = throttler.throttle(socketSource);

    View Slide

  171. Okio
    Socket socket = //...

    Source socketSource = Okio.source(socket);


    Throttler throttler = new Throttler();

    Source throttledSource = throttler.throttle(socketSource);


    BufferedSource bufferedSource = Okio.buffer(throttledSource);

    bufferedSource.readUtf8(10 * 1024);

    View Slide

  172. Okio
    Socket socket = //...

    Source socketSource = Okio.source(socket);


    Throttler throttler = new Throttler();

    Source throttledSource = throttler.throttle(socketSource);


    BufferedSource bufferedSource = Okio.buffer(throttledSource);

    bufferedSource.readUtf8(10 * 1024);


    throttler.bytesPerTimePeriod(50, 1, SECONDS);

    bufferedSource.readUtf8(10 * 1024);

    View Slide

  173. OkHttp

    View Slide

  174. OkHttp
    OkHttpClient client = new OkHttpClient();

    View Slide

  175. OkHttp
    OkHttpClient client = new OkHttpClient();


    Request request = new Request.Builder()

    .url("http://droidcon.ca/a-few-ok-libraries.html")

    .build();

    View Slide

  176. OkHttp
    OkHttpClient client = new OkHttpClient();


    RequestBody requestBody = // ...
    Request request = new Request.Builder()

    .url("http://droidcon.ca/a-few-ok-libraries.html")
    .post(requestBody)

    .build();

    View Slide

  177. OkHttp
    OkHttpClient client = new OkHttpClient();


    RequestBody requestBody = // ...
    Request request = new Request.Builder()

    .url("http://droidcon.ca/a-few-ok-libraries.html")
    .post(requestBody)

    .build();
    class RequestBody {

    static RequestBody create(MediaType mediaType, String body) {}

    static RequestBody create(MediaType mediaType, byte[] body) {}

    static RequestBody create(MediaType mediaType, File body) {}

    }X

    View Slide

  178. OkHttp
    class RequestBody {

    static RequestBody create(MediaType mediaType, String body) {}

    static RequestBody create(MediaType mediaType, byte[] body) {}

    static RequestBody create(MediaType mediaType, File body) {}

    }X

    View Slide

  179. OkHttp
    abstract class RequestBody {

    static RequestBody create(MediaType mediaType, String body) {}

    static RequestBody create(MediaType mediaType, byte[] body) {}

    static RequestBody create(MediaType mediaType, File body) {}


    long contentLength();

    MediaType contentType();

    void writeTo(BufferedSink sink);

    }X

    View Slide

  180. OkHttp
    !
    !
    !
    !
    !
    !
    !
    requestBody.writeTo(sink);

    View Slide

  181. OkHttp
    !
    !
    !
    !
    !
    !
    !
    requestBody.writeTo(sink);
    Socket

    View Slide

  182. OkHttp GET  /a-­‐few-­‐ok-­‐libraries.html  HTTP/1.1  
    Host:  droidcon.ca  
    Content-­‐Type:  text/plain  
    Content-­‐Length:  3000
    !
    !
    !
    !
    !
    !
    !
    requestBody.writeTo(sink);
    Socket

    View Slide

  183. OkHttp GET  /a-­‐few-­‐ok-­‐libraries.html  HTTP/1.1  
    Host:  droidcon.ca  
    Content-­‐Type:  text/plain  
    Content-­‐Length:  3000
    !
    !
    !
    !
    !
    !
    !
    requestBody.writeTo(sink);
    Socket
    FixedLengthSink

    View Slide

  184. OkHttp GET  /a-­‐few-­‐ok-­‐libraries.html  HTTP/1.1  
    Host:  droidcon.ca  
    Content-­‐Type:  text/plain  
    Content-­‐Length:  3000
    !
    !
    !
    !
    !
    !
    !
    requestBody.writeTo(sink);
    Socket
    FixedLengthSink
    !
    !
    !
    !
    !
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.

    View Slide

  185. OkHttp
    Socket
    HTTP/2

    View Slide

  186. OkHttp
    requestBody1
    Socket
    requestBody2
    HTTP/2

    View Slide

  187. OkHttp
    requestBody1
    Socket
    FramedSink FramedSink
    requestBody2
    HTTP/2

    View Slide

  188. OkHttp
    requestBody1
    Socket
    FixedLengthSink
    FramedSink FramedSink
    FixedLengthSink
    requestBody2
    HTTP/2

    View Slide

  189. OkHttp
    requestBody1
    Socket
    FixedLengthSink
    FramedSink FramedSink
    FixedLengthSink
    requestBody2
    HTTP/2

    View Slide

  190. OkHttp
    OkHttpClient client = new OkHttpClient();


    Request request = new Request.Builder()

    .url("http://droidcon.ca/a-few-ok-libraries.html")

    .build();

    View Slide

  191. OkHttp
    OkHttpClient client = new OkHttpClient();


    Request request = new Request.Builder()

    .url("http://droidcon.ca/a-few-ok-libraries.html")

    .build();






    Response response = .execute();
    client.newCall(request)

    View Slide

  192. OkHttp
    OkHttpClient client = new OkHttpClient();


    Request request = new Request.Builder()

    .url("http://droidcon.ca/a-few-ok-libraries.html")

    .build();






    Response response = .execute();








    System.out.println( );
    client.newCall(request)
    response.body().string()

    View Slide

  193. OkHttp






    .enqueue(new Callback() {

    @Override void onResponse(Response response) {


    }


    @Override void onFailure(Request request, IOException e) {

    // ...

    }

    });
    OkHttpClient client = new OkHttpClient();


    Request request = new Request.Builder()

    .url("http://droidcon.ca/a-few-ok-libraries.html")

    .build();








    System.out.println( );
    client.newCall(request)
    response.body().string()

    View Slide

  194. OkHttp
    String string = ;
    response.body().string()

    View Slide

  195. OkHttp
    ResponseBody body = response.body();

    String string = body.string();

    View Slide

  196. OkHttp
    ResponseBody body = response.body();

    String string = body.string();


    Reader reader = body.charStream();

    View Slide

  197. OkHttp
    ResponseBody body = response.body();

    String string = body.string();

    Reader reader = body.charStream();

    InputStream stream = body.byteStream();

    View Slide

  198. OkHttp
    ResponseBody body = response.body();

    String string = body.string();

    Reader reader = body.charStream();

    InputStream stream = body.byteStream();

    byte[] bytes = body.bytes();

    View Slide

  199. OkHttp
    ResponseBody body = response.body();

    String string = body.string();

    Reader reader = body.charStream();

    InputStream stream = body.byteStream();

    byte[] bytes = body.bytes();
    !
    BufferedSource source = body.source();

    View Slide

  200. OkHttp
    !
    !
    !
    !
    !
    !
    !
    body.source();

    View Slide

  201. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000

    View Slide

  202. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000

    View Slide

  203. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000

    View Slide

  204. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000
    FixedLengthSource

    View Slide

  205. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000  
    !
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Length:  3000
    FixedLengthSource

    View Slide

  206. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Encoding:  gzip  
    Content-­‐Length:  3000  
    !
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Content-­‐Encoding:  gzip  
    Content-­‐Length:  3000
    GzipSource
    FixedLengthSource

    View Slide

  207. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Transfer-­‐Encoding:  chunked  
    !
    1F6  
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    18C  
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    158  
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    1EF  
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    1E3  
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    12B  
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    165  
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Transfer-­‐Encoding:  chunked
    ChunkedSource

    View Slide

  208. OkHttp HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Transfer-­‐Encoding:  chunked  
    !
    1F6  
    You  think  water  moves  fast?  You  should  see  ice.  It  
    moves  like  it  has  a  mind.  Like  it  knows  it  killed  the  
    world  once  and  got  a  taste  for  murder.  After  the  
    avalanche,  it  took  us  a  week  to  climb  out.  Now,  I  don't  
    know  exactly  when  we  turned  on  each  other,  but  I  know  
    that  seven  of  us  survived  the  slide...  and  only  five  
    made  it  out.  Now  we  took  an  oath,  that  I'm  breaking  
    now.  We  said  we'd  say  it  was  the  snow  that  killed  the  
    other  two,  but  it  wasn't.  Nature  is  lethal  but  it  
    doesn't  hold  a  candle  to  man.  
    !
    18C  
    Your  bones  don't  break,  mine  do.  That's  clear.  Your  
    cells  react  to  bacteria  and  viruses  differently  than  
    mine.  You  don't  get  sick,  I  do.  That's  also  clear.  But  
    for  some  reason,  you  and  I  react  the  exact  same  way  to  
    water.  We  swallow  it  too  fast,  we  choke.  We  get  some  in  
    our  lungs,  we  drown.  However  unreal  it  may  seem,  we  are  
    connected,  you  and  I.  We're  on  the  same  curve,  just  on  
    opposite  ends.  
    !
    158  
    Normally,  both  your  asses  would  be  dead  as  fucking  
    fried  chicken,  but  you  happen  to  pull  this  shit  while  
    I'm  in  a  transitional  period  so  I  don't  wanna  kill  you,  
    I  wanna  help  you.  But  I  can't  give  you  this  case,  it  
    don't  belong  to  me.  Besides,  I've  already  been  through  
    too  much  shit  this  morning  over  this  case  to  hand  it  
    over  to  your  dumb  ass.  
    !
    1EF  
    The  path  of  the  righteous  man  is  beset  on  all  sides  by  
    the  iniquities  of  the  selfish  and  the  tyranny  of  evil  
    men.  Blessed  is  he  who,  in  the  name  of  charity  and  good  
    will,  shepherds  the  weak  through  the  valley  of  
    darkness,  for  he  is  truly  his  brother's  keeper  and  the  
    finder  of  lost  children.  And  I  will  strike  down  upon  
    thee  with  great  vengeance  and  furious  anger  those  who  
    would  attempt  to  poison  and  destroy  My  brothers.  And  
    you  will  know  My  name  is  the  Lord  when  I  lay  My  
    vengeance  upon  thee.  
    !
    1E3  
    My  money's  in  that  office,  right?  If  she  start  giving  
    me  some  bullshit  about  it  ain't  there,  and  we  got  to  go  
    someplace  else  and  get  it,  I'm  gonna  shoot  you  in  the  
    head  then  and  there.  Then  I'm  gonna  shoot  that  bitch  in  
    the  kneecaps,  find  out  where  my  goddamn  money  is.  She  
    gonna  tell  me  too.  Hey,  look  at  me  when  I'm  talking  to  
    you,  motherfucker.  You  listen:  we  go  in  there,  and  that  
    nigga  Winston  or  anybody  else  is  in  there,  you  the  
    first  motherfucker  to  get  shot.  You  understand?  
    !
    12B  
    Look,  just  because  I  don't  be  givin'  no  man  a  foot  
    massage  don't  make  it  right  for  Marsellus  to  throw  
    Antwone  into  a  glass  motherfuckin'  house,  fuckin'  up  
    the  way  the  nigger  talks.  Motherfucker  do  that  shit  to  
    me,  he  better  paralyze  my  ass,  'cause  I'll  kill  the  
    motherfucker,  know  what  I'm  sayin’?  
    !
    165  
    Well,  the  way  they  make  shows  is,  they  make  one  show.  
    That  show's  called  a  pilot.  Then  they  show  that  show  to  
    the  people  who  make  shows,  and  on  the  strength  of  that  
    one  show  they  decide  if  they're  going  to  make  more  
    shows.  Some  pilots  get  picked  and  become  television  
    programs.  Some  don't,  become  nothing.  She  starred  in  
    one  of  the  ones  that  became  nothing.
    !
    !
    !
    !
    !
    !
    !
    body.source();
    Socket
    HTTP/1.1  200  OK  
    Date:  Thu,  9  Apr  2015  15:14:34  GMT  
    Server:  JakeWharton/2.3.7  (Human)  
    Content-­‐Type:  application/presentation;  charset=UTF-­‐8  
    Transfer-­‐Encoding:  chunked
    ChunkedSource

    View Slide

  209. OkHttp
    Socket
    HTTP/2

    View Slide

  210. OkHttp
    Socket
    HTTP/2

    View Slide

  211. OkHttp
    Socket
    FramedSource FramedSource
    HTTP/2

    View Slide

  212. OkHttp
    Socket
    FixedLengthSource
    FramedSource FramedSource
    FixedLengthSource
    HTTP/2

    View Slide

  213. OkHttp
    body1.source();
    Socket
    FixedLengthSource
    FramedSource FramedSource
    FixedLengthSource
    body2.source();
    HTTP/2

    View Slide

  214. Moshi

    View Slide

  215. Moshi
    • JsonReader / JsonWriter for streaming

    View Slide

  216. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    View Slide

  217. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject(); {

    View Slide

  218. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");
    {  
       "title":  "A  Few  'Ok'  Libraries"

    View Slide

  219. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");
    {  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton"

    View Slide

  220. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");

    writer.name("tags")

    .beginArray();
    {  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [

    View Slide

  221. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");

    writer.name("tags")

    .beginArray()

    .value("Okio")

    .value("OkHttp")

    .value("Moshi")

    .value("Retrofit");
    {  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit

    View Slide

  222. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");

    writer.name("tags")

    .beginArray()

    .value("Okio")

    .value("OkHttp")

    .value("Moshi")

    .value("Retrofit")

    .endArray();
    {  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]

    View Slide

  223. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");

    writer.name("tags")

    .beginArray()

    .value("Okio")

    .value("OkHttp")

    .value("Moshi")

    .value("Retrofit")

    .endArray();

    writer.endObject();
    {  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]  
    }

    View Slide

  224. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");

    writer.name("tags")

    .beginArray()

    .value("Okio")

    .value("OkHttp")

    .value("Moshi")

    .value("Retrofit")

    .endArray();

    writer.endObject();

    writer.close();
    {  X  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]  Y  
    }  Z

    View Slide

  225. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");

    writer.name("tags")

    .beginArray()

    .value("Okio")

    .value("OkHttp")

    .value("Moshi")

    .value("Retrofit")

    .endArray();

    writer.endObject();

    writer.close();
    Sink sink = //...

    BufferedSink s = Okio.buffer(sink);

    s.writeByte('{');

    s.writeByte('"').writeUtf8("title").writeByte('"');

    s.writeByte(':').writeByte('"')

    .writeUtf8("A Few 'Ok' Libraries")

    .writeByte('"');

    s.writeByte(',');

    s.writeByte('"').writeUtf8("presenter").writeByte('"');

    s.writeByte(':').writeByte('"')

    .writeUtf8("Jake Wharton")

    .writeByte('"');

    s.writeByte(',');

    s.writeByte('"').writeUtf8("tags").writeByte('"');

    s.writeByte(':').writeByte('[');

    s.writeByte('"').writeUtf8("Okio").writeByte('"');

    s.writeByte(',');
    s.writeByte('"').writeUtf8("OkHttp").writeByte('"');

    s.writeByte(',');
    s.writeByte('"').writeUtf8("Moshi").writeByte('"');

    s.writeByte(',');
    s.writeByte('"').writeUtf8("Retrofit").writeByte('"');

    s.writeByte(']');

    s.writeByte('}');

    s.close();

    View Slide

  226. Moshi
    Sink sink = //...

    JsonWriter writer = new JsonWriter(sink);

    writer.beginObject();

    writer.name("title")

    .value("A Few 'Ok' Libraries");

    writer.name("presenter")

    .value("Jake Wharton");

    writer.name("tags")

    .beginArray()

    .value("Okio")

    .value("OkHttp")

    .value("Moshi")

    .value("Retrofit")

    .endArray();

    writer.endObject();

    writer.close();
    {X  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [X  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]X  
    }X

    View Slide

  227. Moshi
    Source source = //...

    JsonReader reader = new JsonReader(source);
    {X  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]X  
    }X

    View Slide

  228. Moshi
    Source source = //...

    JsonReader reader = new JsonReader(source);


    reader.beginObject();

    while (reader.peek() != Token.END_OBJECT) {

    switch (reader.nextName()) {

    case "title":

    String title = reader.nextString();

    // TODO do something with 'title'...

    break;

    case "presenter":

    String presenter = reader.nextString();

    // TODO do something with 'presenter'...

    break;

    case "tags":

    reader.beginArray();

    Set tags = new LinkedHashSet<>();

    while (reader.peek() != Token.END_ARRAY) {

    tags.add(reader.nextString());

    }

    reader.endArray();

    // TODO do something with 'tags'...

    break;

    default: break; // Ignored

    }

    }

    reader.endObject();

    reader.close();
    {X  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]X  
    }X

    View Slide

  229. Moshi
    Source source = //...

    JsonReader reader = new JsonReader(source);


    reader.beginObject();

    while (reader.peek() != Token.END_OBJECT) {

    switch (reader.nextName()) {

    case "title":

    String title = reader.nextString();

    // TODO do something with 'title'...

    break;

    case "presenter":

    String presenter = reader.nextString();

    // TODO do something with 'presenter'...

    break;

    case "tags":

    reader.beginArray();

    Set tags = new LinkedHashSet<>();

    while (reader.peek() != Token.END_ARRAY) {

    tags.add(reader.nextString());

    }

    reader.endArray();

    // TODO do something with 'tags'...

    break;

    default: break; // Ignored

    }

    }

    reader.endObject();

    reader.close();
    static final ByteString DOUBLE_QUOTE =
    ByteString.encodeUtf8("\"");


    long i = s.indexOfElement(DOUBLE_QUOTE);

    String string = s.readUtf8(i);

    View Slide

  230. Moshi
    • JsonReader / JsonWriter for streaming

    View Slide

  231. Moshi
    • JsonReader / JsonWriter for streaming
    • JsonAdapter for object mapping

    View Slide

  232. Moshi
    {X  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [X  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]X  
    }X

    View Slide

  233. Moshi
    {X  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [X  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]X  
    }X
    class Presentation {

    String title;

    String presenter;

    Set tags;

    }

    View Slide

  234. Moshi
    {X  
       "title":  "A  Few  'Ok'  Libraries",  
       "presenter":  "Jake  Wharton",  
       "tags":  [X  
           "Okio",  
           "OkHttp",  
           "Moshi",  
           "Retrofit  
       ]X  
    }X
    class Presentation {

    String title;

    String presenter;

    Set tags;

    }
    JsonAdapter adapter
    = moshi.adapter(Presentation.class);

    Presentation presentation =
    adapter.fromJson(source);

    View Slide

  235. Retrofit

    View Slide

  236. Retrofit
    interface DroidconMtl {

    @GET("/a-few-ok-libraries.json")

    Presentation okLibraries();

    }

    View Slide

  237. Retrofit
    interface DroidconMtl {

    @GET("/a-few-ok-libraries.json")

    Presentation okLibraries();

    }
    class Presentation {

    String title;

    String presenter;

    Set tags;

    }

    View Slide

  238. Retrofit
    interface DroidconMtl {

    @GET("/a-few-ok-libraries.json")

    Presentation okLibraries();

    }
    RestAdapter ra = new RestAdapter.Builder()

    .setEndpoint("http://droidcon.ca")

    .build();

    View Slide

  239. Retrofit
    interface DroidconMtl {

    @GET("/a-few-ok-libraries.json")

    Presentation okLibraries();

    }
    RestAdapter ra = new RestAdapter.Builder()

    .setEndpoint("http://droidcon.ca")

    .build();




    DroidconMtl droidconMtl = ra.create(DroidconMtl.class);

    View Slide

  240. Retrofit
    interface DroidconMtl {

    @GET("/a-few-ok-libraries.json")

    Presentation okLibraries();

    }
    RestAdapter ra = new RestAdapter.Builder()

    .setEndpoint("http://droidcon.ca")

    .build();




    DroidconMtl droidconMtl = ra.create(DroidconMtl.class);



    !

    Presentation ok = droidconMtl.okLibraries();

    View Slide

  241. Retrofit
    RestAdapter ra = new RestAdapter.Builder()

    .setEndpoint("http://droidcon.ca")

    .build();

    View Slide

  242. Retrofit
    RestAdapter ra = new RestAdapter.Builder()

    .setEndpoint("http://droidcon.ca")

    .setClient(new OkHttpClient())

    .build();

    View Slide

  243. Retrofit
    RestAdapter ra = new RestAdapter.Builder()

    .setEndpoint("http://droidcon.ca")

    .setClient(new OkHttpClient())

    .setConverter(new MoshiConverter())

    .build();

    View Slide

  244. Retrofit

    View Slide

  245. Retrofit OkHttp

    View Slide

  246. Retrofit OkHttp
    Socket

    View Slide

  247. Retrofit OkHttp
    FixedLengthSource
    FixedLengthSink
    Socket

    View Slide

  248. Retrofit OkHttp
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody
    Socket

    View Slide

  249. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody
    Socket

    View Slide

  250. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody
    Socket
    Moshi

    View Slide

  251. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody
    Socket
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  252. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    RequestBody
    ResponseBody
    Socket
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  253. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    DroidconMtl
    RequestBody
    ResponseBody
    Socket
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  254. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    DroidconMtl
    RequestBody
    ResponseBody
    Socket
    okLibraries()
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  255. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    DroidconMtl
    RequestBody
    ResponseBody
    Socket
    execute()
    okLibraries()
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  256. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    DroidconMtl
    RequestBody
    ResponseBody
    Socket
    execute()
    okLibraries()
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  257. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    DroidconMtl
    RequestBody
    ResponseBody
    Socket
    execute()
    okLibraries()
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  258. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    DroidconMtl
    RequestBody
    ResponseBody
    Socket
    execute()
    okLibraries()
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  259. Retrofit OkHttp
    MoshiConverter
    FixedLengthSource
    FixedLengthSink
    RequestBody
    ResponseBody

    DroidconMtl
    RequestBody
    ResponseBody
    Socket
    execute()
    okLibraries()
    Moshi
    BufferedSink
    BufferedSource

    View Slide

  260. okio
    okhttp
    moshi
    retrofit

    View Slide

  261. A Few “OK” Libraries

    View Slide

  262. A Few “OK” Libraries
    jakewharton
    jakewharton
    jakewharton
    twitter.com/
    google.com/+
    .com

    View Slide