Lines Matching +full:java +full:- +full:docs +full:- +full:samples
5 them to learn about how everything works together. Cut-and-paste these examples freely; that's what
8 These recipes work on all platforms: Java, Android, Kotlin/Native, and Kotlin/JS. See
9 [java.io Recipes](java_io_recipes.md) for samples that integrate Java APIs.
12 Read a text file line-by-line ([Java][ReadFileLineByLine]/[Kotlin][ReadFileLineByLineKt])
13 -----------------------------------------------------------------------------------------
28 === "Java"
30 Here we use Java's `try` blocks to close our sources automatically.
32 ```java
57 FileSystem.SYSTEM.source(path).use { fileSource ->
58 fileSource.buffer().use { bufferedFileSource ->
77 === "Java"
79 The above Java program can be written more compactly by inlining the `fileSource` variable and
82 ```java
113 The `readUtf8Line()` method is suitable for parsing most files. For certain use-cases you may also
118 === "Java"
120 ```java
150 Write a text file ([Java][WriteFile]/[Kotlin][WriteFileKt])
151 -----------------------------------------------------------
156 ```java
177 === "Java"
182 ```java
217 ```java
222 UTF-8 ([Java][ExploreCharsets]/[Kotlin][ExploreCharsetsKt])
223 -----------------------------------------------------------
225 In the above APIs you can see that Okio really likes UTF-8. Early computer systems suffered many
226 incompatible character encodings: ISO-8859-1, ShiftJIS, ASCII, EBCDIC, etc. Writing software to
228 world has standardized on UTF-8 everywhere, with some rare uses of other charsets in legacy systems.
232 is only readable by the local computer. Most programs should use the UTF-8 methods only.
239 Though we use UTF-8 whenever we read or write strings in I/O, when they are in memory Java Strings
240 use an obsolete character encoding called UTF-16. It is a bad encoding because it uses a 16-bit
241 `char` for most characters, but some don’t fit. In particular, most emoji use two Java chars. This
242 is problematic because `String.length()` returns a surprising result: the number of UTF-16 chars and
246 | --------------------: | :---------------------------| :------------------------------|
249 | UTF-8 bytes | `43 61 66 c3a9 20 f09f8da9` | `43 61 66 65 cc81 20 f09f8da9` |
255 them, there are convenient APIs for dealing with low-level UTF-8 strings.
257 Use `Utf8.size()` to count the number of bytes required to encode a string as UTF-8 without actually
258 encoding it. This is handy in length-prefixed encodings like protocol buffers.
260 Use `BufferedSource.readUtf8CodePoint()` to read a single variable-length code point, and
264 === "Java"
266 ```java
272 System.out.println(" UTF-8 bytes: " + ByteString.encodeUtf8(s).hex());
285 println(" UTF-8 bytes: " + s.encodeUtf8().hex())
290 Golden Values ([Java][GoldenValue]/[Kotlin][GoldenValueKt])
291 -----------------------------------------------------------
298 We’ll illustrate this by encoding a value using Java Serialization. Though we must disclaim that
299 Java Serialization is an awful encoding system and most programs should prefer other formats like
303 === "Java"
305 ```Java
321 ObjectOutputStream(buffer.outputStream()).use { objectOut ->
336 3. We create an `ObjectOutputStream` (the encoding API for Java serialization) and write our object.
345 === "Java"
347 ```Java
371 === "Java"
373 ```Java
390 === "Java"
392 ```Java
409 ObjectInputStream(buffer.inputStream()).use { objectIn ->
417 === "Java"
419 ```Java
440 Write a binary file ([Java][BitmapEncoder]/[Kotlin][BitmapEncoderKt])
441 ---------------------------------------------------------------------
455 whether the bytes are ordered most-significant to least (big endian) or least-significant to most
456 (little endian). Okio uses the `Le` suffix for little-endian methods; methods without a suffix
457 are big-endian.
459 * **Signed vs. Unsigned.** Java doesn’t have unsigned primitive types (except for `char`!) so
465 | :----------- | ----: | :--------- | --------------: | :------------------------ |
483 === "Java"
485 ```Java
518 for (int y = height - 1; y >= 0; y--) {
525 // Padding for 4-byte alignment.
568 for (y in height - 1 downTo 0) {
575 // Padding for 4-byte alignment.
584 to begin on a 4-byte boundary so it is necessary to add zeros to maintain the alignment.
591 length-prefixed formats.
595 Communicate on a Socket ([Java][SocksProxyServer]/[Kotlin][SocksProxyServerKt])
596 -------------------------------------------------------------------------------
613 manually call `flush()` to transmit data. Typically message-oriented protocols flush after each
617 Okio builds on `java.io.Socket` for connectivity. Create your socket as a server or as a client,
629 === "Java"
631 ```Java
648 === "Java"
650 ```Java
652 for (long byteCount; (byteCount = source.read(buffer, 8192L)) != -1; ) {
663 while (source.read(buffer, 8192L).also { byteCount = it } != -1L) {
676 === "Java"
678 ```Java
691 bitwise `&` operator is Java’s preferred idiom to convert a signed value into an unsigned value.
695 | :---- | :---------------------------: | :--------------- | :-------------------------- |
696 | byte | -128..127 | 0..255 | `int u = s & 0xff;` |
697 | short | -32,768..32,767 | 0..65,535 | `int u = s & 0xffff;` |
698 | int | -2,147,483,648..2,147,483,647 | 0..4,294,967,295 | `long u = s & 0xffffffffL;` |
700 Java has no primitive type that can represent unsigned longs.
703 Hashing ([Java][Hashing]/[Kotlin][HashingKt])
704 ---------------------------------------------
706 We’re bombarded by hashing in our lives as Java programmers. Early on we're introduced to the
716 Each cryptographic hash function accepts a variable-length stream of input bytes and produces a
717 fixed-length byte string value called the “hash”. Hash functions have these important qualities:
722 * Non-reversible: knowing an output doesn't help you to find the input. Note that if you know some
724 * Well-known: the hash is implemented everywhere and rigorously understood.
727 (quintillions of millenia). Steady advances in computing and mathematics have caused once-great hash
729 created equal! Okio supports these well-known cryptographic hash functions:
731 * **MD5**: a 128-bit (16 byte) cryptographic hash. It is both insecure and obsolete because it is
733 legacy systems that are not security-sensitive.
734 * **SHA-1**: a 160-bit (20 byte) cryptographic hash. It was recently demonstrated that it is
735 feasible to create SHA-1 collisions. Consider upgrading from SHA-1 to SHA-256.
736 * **SHA-256**: a 256-bit (32 byte) cryptographic hash. SHA-256 is widely understood and expensive
738 * **SHA-512**: a 512-bit (64 byte) cryptographic hash. It is expensive to reverse.
741 human-readable form. Or leave it as a `ByteString` because that’s a convenient model type!
745 === "Java"
747 ```Java
767 === "Java"
769 ```Java
789 === "Java"
791 ```Java
802 sha256(blackholeSink()).use { hashingSink ->
803 FileSystem.SYSTEM.source(path).buffer().use { source ->
812 === "Java"
814 ```Java
827 sha256(blackholeSink()).use { hashingSink ->
828 hashingSink.buffer().use { sink ->
829 FileSystem.SYSTEM.source(path).use { source ->
841 === "Java"
843 ```Java
858 On Android and Java, Okio uses Java’s `java.security.MessageDigest` for cryptographic hashes and
864 -------------------------
866 On Android and Java it's easy to encrypt streams.
869 chosen algorithm, the key, and algorithm-specific additional parameters like the initialization
873 === "Java"
878 ```java
925 [base64]: https://tools.ietf.org/html/rfc4648#section-4
927 [nfd]: https://docs.oracle.com/javase/7/docs/api/java/text/Normalizer.Form.html#NFD
928 [nfc]: https://docs.oracle.com/javase/7/docs/api/java/text/Normalizer.Form.html#NFC
929 …apEncoderKt]: https://github.com/square/okio/blob/master/samples/src/jvmMain/kotlin/okio/samples/B…
930 …er]: https://github.com/square/okio/blob/master/samples/src/jvmMain/java/okio/samples/BitmapEncode…
931 …eCharsetsKt]: https://github.com/square/okio/blob/master/samples/src/jvmMain/kotlin/okio/samples/E…
932 …s]: https://github.com/square/okio/blob/master/samples/src/jvmMain/java/okio/samples/ExploreCharse…
933 [GoldenValueKt]: https://github.com/square/okio/blob/master/samples/src/jvmMain/kotlin/okio/samples…
934 …lue]: https://github.com/square/okio/blob/master/samples/src/jvmMain/java/okio/samples/GoldenValue…
935 [HashingKt]: https://github.com/square/okio/blob/master/samples/src/jvmMain/kotlin/okio/samples/Has…
936 …shing]: https://github.com/square/okio/blob/master/samples/src/jvmMain/java/okio/samples/Hashing.j…
937 …]: https://github.com/square/okio/blob/master/samples/src/jvmMain/java/okio/samples/ReadFileLineBy…
938 …ineByLineKt]: https://github.com/square/okio/blob/master/samples/src/jvmMain/kotlin/okio/samples/R…
939 …oxyServerKt]: https://github.com/square/okio/blob/master/samples/src/jvmMain/kotlin/okio/samples/S…
940 …r]: https://github.com/square/okio/blob/master/samples/src/jvmMain/java/okio/samples/SocksProxySer…
941 …File]: https://github.com/square/okio/blob/master/samples/src/jvmMain/java/okio/samples/WriteFile.…
942 [WriteFileKt]: https://github.com/square/okio/blob/master/samples/src/jvmMain/kotlin/okio/samples/W…