{"id":16627112,"url":"https://github.com/ttulka/boundary-io-streams","last_synced_at":"2025-03-11T13:32:31.954Z","repository":{"id":57730148,"uuid":"109155234","full_name":"ttulka/boundary-io-streams","owner":"ttulka","description":"Boundary I/O Streams Java Library","archived":false,"fork":false,"pushed_at":"2018-09-20T19:25:38.000Z","size":185,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-18T03:08:16.547Z","etag":null,"topics":["boundary","boundary-stream","input-output","io","java","java-library","stream","stream-processing","streaming"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ttulka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-01T16:26:50.000Z","updated_at":"2024-04-03T23:37:19.000Z","dependencies_parsed_at":"2022-09-07T20:31:15.058Z","dependency_job_id":null,"html_url":"https://github.com/ttulka/boundary-io-streams","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Fboundary-io-streams","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Fboundary-io-streams/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Fboundary-io-streams/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Fboundary-io-streams/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ttulka","download_url":"https://codeload.github.com/ttulka/boundary-io-streams/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243042891,"owners_count":20226718,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boundary","boundary-stream","input-output","io","java","java-library","stream","stream-processing","streaming"],"created_at":"2024-10-12T04:13:25.745Z","updated_at":"2025-03-11T13:32:31.917Z","avatar_url":"https://github.com/ttulka.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Boundary I/O Streams\n\n**Java library for boundary I/O streams.**\n\n- Separate sub-stream from one stream with a boundary.\n- Read multiple sub-stream from one stream based on a boundary.\n\nA stream\n```\n+-------------------+----------+-----+-------------------+----------+----+\n| sub-stream 1 data | boundary | ... | sub-stream N data | boundary | -1 |  \n+-------------------+----------+-----+-------------------+----------+----+\n```\nwill be read as\n```\n+-------------------+----+-----+-------------------+----+\n| sub-stream 1 data | -1 | ... | sub-stream N data | -1 | finished \n+-------------------+----+-----+-------------------+----+\n```\n\nPlease note the last boundary is not necessary but recommended for a good streaming design.\n\n## Prerequisites\n- Java 6\n\n## Usage\n\nCopy the Maven dependency into your Maven project:\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecz.net21.ttulka.io\u003c/groupId\u003e\n    \u003cartifactId\u003eboundary-io-streams\u003c/artifactId\u003e\n    \u003cversion\u003e1.2.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Read from a Boundary Stream\n\n#### Create a boundary input stream:\n```java\nBoundaryInputStream bis = null;\ntry {\n    // create the bis from a file input stream\n    FileInputStream fis = new FileInputStream(\"test.dat\");\n    bis = new BoundaryInputStream(fis);  // or new BoundaryInputStream(fis, boundary) with an explicit boundary\n    // ...\n\n} finally {\n    // closes the base 'fis' automatically\n    bis.close();\n}\n```\n\n#### Iterate through multiple streams:\n```java\nfor (InputStream is : bis) {\n    int b;\n    while ((b = is.read()) != -1) {\n        // ...\n    }\n}\n```\n\n#### Use the multiple stream iterator:\n```java\nIterator\u003cInputStream\u003e it = bis.iterator();\nif (it.hasNext()) {\n    InputStream is = it.next();\n    \n    int b;\n    while ((b = is.read()) != -1) {\n        // ...\n    }\n}\n```\n\n#### Alternatively you can work with the boundary input stream directly:\n```java\nwhile (!bis.hasFinished()) {\n    bis.next();\n    \n    int b;\n    while ((b = bis.read()) != -1) {\n        // ...\n    }\n}\n```\n\n### Write into a Boundary Stream\n\n#### Create a boundary output stream:\n```java\nBoundaryOutputStream bos = null;\ntry {\n    // create the bos from a file output stream \n    FileOutputStream fos = new FileOutputStream(\"test.dat\");\n    bos = new BoundaryOutputStream(fos);  // or 'new BoundaryOutputStream(fos, boundary)' with an explicit boundary  \n    // ...\n\n} finally {\n    // closes the base 'fos' automatically\n    bos.close();\n}\n```\n\n#### Write multiple streams into a boundary output stream:\n```java\nbyte[] subStream1 = ...\nbos.write(subStream1);\nbos.boundary(); // write the boundary after the first sub-stream\n\nbyte[] subStream2 = ...\nbos.write(subStream2);\nbos.boundary(); // write the boundary after the second sub-stream\n```\n\nMethod `boundary()` is only convenient and identical to the following code:\n```java\nbyte[] boundary = ...\nos.write(boundary);\n```\nSo it's not necessary to create the stream via `BoundaryOutputStream` for reading it via `BoundaryInputStream`.\n\n### Stop Boundary Stream\n\nTo stop consuming a stream after a boundary was reached it is possible to use the `StopBoundaryInputStream` class and the convenience class `StopBoundaryOutputStream` to generate such a stream.\nThe stop boundary streaming is still using a boundary to separate sub-streams, but when a stop boundary occurs the rest of the input stream is ignored.\n\nThe manner of work is same as with `BoundaryInputStream`, resp. `BoundaryOutputStream`.\n\n#### Example:\n```java\nString[] values = {\n        \"abcde\", \"ABCDE\", \"12345\"\n};\n\nbyte[] boundary = \"|\".getBytes();\nbyte[] stopBoundary = \"#\".getBytes();\n\nByteArrayOutputStream bytes = new ByteArrayOutputStream();\n\nStopBoundaryOutputStream out = new StopBoundaryOutputStream(bytes, boundary, stopBoundary);\n\nfor (String s : values) {\n    out.write(s.getBytes());\n    out.boundary();\n}\nout.stopBoundary();\nout.write(\"xyz\".getBytes());    // some junk at the end\n\n//System.out.println(bytes);    // prints `abcde|ABCDE|12345|#xyz`\n\nStopBoundaryInputStream in = new StopBoundaryInputStream(\n        new ByteArrayInputStream(bytes.toByteArray()), boundary, stopBoundary);\n\nfor (InputStream is : in) {\n    int b;\n    while ((b = is.read()) != -1) {\n        System.out.print((char) b);\n    }\n    System.out.println();\n}\n\n// close streams...\n```\nThe code above prints:\n```\nabcde\nABCDE\n12345\n```\nThe last string `xyz` is ignored after the stop boundary was reached.\n\n## Release Changes\n\n### 1.2.0\n- `StopBoundaryInputStream` and `StopBoundaryOutputStream` classes added.\n\n### 1.1.0\n- `BoundaryInputStream` implements `Iterable\u003cInputStream\u003e`.\n- `IterableBoundaryInputStream` class removed as obsolete.\n- Bugfix: `hasFinished() == true` after calling `close()`.\n\n### 1.0.0\nInitial version\n\n## License\n\n[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttulka%2Fboundary-io-streams","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fttulka%2Fboundary-io-streams","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttulka%2Fboundary-io-streams/lists"}