Skip to content

Commit 9e54b4c

Browse files
committed
ReaderInputStream バッファサイズ調整
Central対応
1 parent 39c93e8 commit 9e54b4c

File tree

6 files changed

+173
-107
lines changed

6 files changed

+173
-107
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
# SoftLib
2-
簡単なデータ梱包などの一部公開
3-
4-
SoftLibABNF、SoftLibJSONで利用する部分程度の公開
5-
6-
## BASE64
7-
8-
3種類のBASE64っぽいフォーマットが読み書きできる割と速いものです
9-
10-
## Packet
11-
12-
可変長配列、のようなものを目指してみたらこうなった。
13-
14-
頭と尻から読み書きできるのでFIFOでもLIFOでもできるような構造
1+
# SoftLib
2+
簡単なデータ梱包構造などの一部
3+
Simple Stream Packet
4+
5+
SoftLibABNF、SoftLibJSONで利用する部分程度の公開
6+
7+
## BASE64
8+
9+
3種類のBASE64っぽいフォーマットが読み書きできる割と速いものです
10+
11+
## Packet
12+
13+
可変長配列、のようなものを目指してみたらこうなった。
14+
15+
FrontPacket, BackPacketが頭と尻のようなもので双方でInputStream,OutputStreamっぽいものが使える。
16+
FIFOでもLIFOでもできるような抽象構造。
17+
中身は配列のチェーンだがTEMPファイルなどにすると巨大化も期待できる。

pom.xml

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<name>SoftLib</name>
54
<groupId>net.siisise</groupId>
65
<artifactId>softlib</artifactId>
7-
<version>1.1.2</version>
6+
<version>1.1.4</version>
87
<packaging>jar</packaging>
8+
<name>SoftLib</name>
9+
<description>Java simple API Packet io, base64, etc...</description>
10+
<url>https://github.com/okomeki/SoftLib</url>
11+
<licenses>
12+
<license>
13+
<name>The Apache License, Version 2.0</name>
14+
<url>http://www.apache.org/license/LICENSE-2.0.txt</url>
15+
</license>
16+
</licenses>
17+
<developers>
18+
<developer>
19+
<name>SATO Masatoshi</name>
20+
<email>okome@siisise.net</email>
21+
<organization>Siisise Net</organization>
22+
</developer>
23+
</developers>
924
<build>
1025
<plugins>
1126
<plugin>
@@ -14,37 +29,70 @@
1429
<version>3.2.1</version>
1530
<executions>
1631
<execution>
32+
<id>attach-sources</id>
1733
<phase>package</phase>
1834
<goals>
19-
<goal>jar</goal>
35+
<goal>jar-no-fork</goal>
2036
</goals>
2137
</execution>
2238
</executions>
2339
</plugin>
2440
<plugin>
2541
<groupId>org.apache.maven.plugins</groupId>
2642
<artifactId>maven-compiler-plugin</artifactId>
27-
<version>3.8.1</version>
43+
<version>3.9.0</version>
2844
</plugin>
2945
<plugin>
3046
<groupId>org.apache.maven.plugins</groupId>
3147
<artifactId>maven-javadoc-plugin</artifactId>
3248
<version>3.3.1</version>
49+
<!--
3350
<configuration>
3451
<source>1.8</source>
3552
</configuration>
53+
-->
3654
<executions>
3755
<execution>
56+
<id>attach-javadocs</id>
3857
<phase>package</phase>
3958
<goals>
4059
<goal>jar</goal>
4160
</goals>
4261
</execution>
4362
</executions>
4463
</plugin>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-gpg-plugin</artifactId>
67+
<version>3.0.1</version>
68+
<executions>
69+
<execution>
70+
<id>sign-artifacts</id>
71+
<phase>verify</phase>
72+
<goals>
73+
<goal>sign</goal>
74+
</goals>
75+
</execution>
76+
</executions>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.sonatype.plugins</groupId>
80+
<artifactId>nexus-staging-maven-plugin</artifactId>
81+
<version>1.6.8</version>
82+
<extensions>true</extensions>
83+
<configuration>
84+
<serverId>ossrh</serverId>
85+
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
86+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
87+
</configuration>
88+
</plugin>
4589
</plugins>
4690
</build>
4791
<distributionManagement>
92+
<snapshotRepository>
93+
<id>ossrh</id>
94+
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
95+
</snapshotRepository>
4896
<repository>
4997
<id>github</id>
5098
<name>GitHub okomeki Apache Maven SoftLib Packages</name>

src/main/java/net/siisise/io/BaseBitPac.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public int read(byte[] data, int offset, int length) {
5454
}
5555

5656
public abstract int readInt(int bit);
57-
5857
public abstract long readBit(byte[] data, long offset, long bitLength);
5958
public abstract BitPacket readPac(int bitLength);
6059

@@ -87,8 +86,9 @@ public void writeBit(BitPacket pac) {
8786

8887
/**
8988
* ToDo: 端数のbig/littleが異なる場合
89+
*
9090
* @param bp
91-
* @param bitLength
91+
* @param bitLength
9292
*/
9393
public void writeBit(BitPacket bp, long bitLength) {
9494
byte[] data = new byte[(int) (bitLength + 7) / 8];
@@ -97,7 +97,6 @@ public void writeBit(BitPacket bp, long bitLength) {
9797
}
9898

9999
public abstract void writeBit(int data, int bitLength);
100-
101100
public abstract void writeBit(byte[] data, long bitOffset, long bitLength);
102101
}
103102

@@ -131,6 +130,11 @@ public long bitLength() {
131130
return pac.length() * 8 - readPadding - writePadding;
132131
}
133132

133+
/**
134+
* 端数は含まないバイト列として返せる値.
135+
*
136+
* @return
137+
*/
134138
@Override
135139
public long length() {
136140
return bitLength() / 8;
@@ -144,7 +148,7 @@ public int size() {
144148

145149
/**
146150
*
147-
* @param len
151+
* @param len 0~32くらい
148152
* @return
149153
*/
150154
static final int andMask(int len) {
@@ -164,7 +168,8 @@ public int read() {
164168
}
165169

166170
/**
167-
* 8ビット単位で転送. 端数は残る。
171+
* 8ビット単位で転送.
172+
* 端数は残る。
168173
*
169174
* @param data 転送先配列
170175
* @return 転送可能なバイト長。
@@ -190,7 +195,8 @@ public int readInt(int bit) {
190195
}
191196

192197
/**
193-
* readのビット版 入れ物はbyte列
198+
* readのビット版
199+
* 入れ物はbyte列
194200
* 左詰め |01234567|89ABCDEF|
195201
*
196202
* @param data 戻りデータ
@@ -259,7 +265,7 @@ public void write(byte[] data) {
259265
public void write(byte[] data, int offset, int length) {
260266
out.write(data, offset, length);
261267
}
262-
268+
263269
@Override
264270
public void write(FrontPacket pac) {
265271
write(pac.toByteArray());

src/main/java/net/siisise/io/BitPacket.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
* javaのBitSetと互換かなにかにしたい
5-
*
5+
* ビット系からバイト系に変換する際には端数ビットを捨てる方向で調整する。
6+
* 必要ならパディングを入れてみよう。
67
*/
78
public interface BitPacket extends FrontPacket,BackPacket {
89

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,88 @@
1-
package net.siisise.io;
2-
3-
import java.io.IOException;
4-
import java.io.InputStream;
5-
import java.io.Reader;
6-
import java.nio.charset.StandardCharsets;
7-
import net.siisise.lang.CodePoint;
8-
9-
/**
10-
* InputStreamReaderの逆
11-
* とりあえずUTF-8出力前提
12-
* 1000文字程度読んでおく
13-
*/
14-
public class ReaderInputStream extends InputStream {
15-
16-
final PacketA pac = new PacketA();
17-
char[] pair;
18-
final Reader rd;
19-
boolean eof = false;
20-
21-
ReaderInputStream(Reader r) {
22-
rd = r;
23-
}
24-
25-
private void buffering() throws IOException {
26-
if ( eof ) return;
27-
int ch = rd.read();
28-
if ( ch < 0 ) {
29-
eof = true;
30-
return;
31-
}
32-
if ( ch >= 0xd800 && ch <= 0xdbff ) {
33-
if ( pair != null ) {
34-
ch = pair[0];
35-
byte[] bytes = CodePoint.utf8(ch);
36-
pac.write(bytes);
37-
}
38-
pair = new char[] {(char)ch,0};
39-
buffering();
40-
return;
41-
} else if ( ch >= 0xdc00 && ch <= 0xdfff ) {
42-
byte[] bytes;
43-
if ( pair != null ) {
44-
pair[1] = (char)ch;
45-
bytes = String.valueOf(pair).getBytes(StandardCharsets.UTF_8);
46-
} else {
47-
bytes = CodePoint.utf8(ch);
48-
}
49-
pac.write(bytes);
50-
} else {
51-
pair = null;
52-
byte[] bytes = CodePoint.utf8(ch);
53-
pac.write(bytes);
54-
}
55-
}
56-
57-
@Override
58-
public int read() throws IOException {
59-
while ( !eof && pac.size() < 1024 ) {
60-
buffering();
61-
}
62-
return pac.read();
63-
}
64-
65-
@Override
66-
public void close() throws IOException {
67-
rd.close();
68-
}
69-
70-
/**
71-
* 正確な長さがわからない
72-
* @return
73-
* @throws IOException
74-
*/
75-
@Override
76-
public int available() throws IOException {
77-
return pac.size() + (rd.ready() ? 1 : 0);
78-
}
79-
80-
}
1+
package net.siisise.io;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.Reader;
6+
import java.nio.charset.StandardCharsets;
7+
import net.siisise.lang.CodePoint;
8+
9+
/**
10+
* バイト列が必要な処理系にReader系を繋ぐ
11+
* InputStreamReaderの逆
12+
* とりあえずUTF-8出力前提
13+
* 100文字程度読んでおく
14+
*/
15+
public class ReaderInputStream extends InputStream {
16+
17+
final PacketA pac = new PacketA();
18+
char[] pair;
19+
final Reader rd;
20+
final int bufferSize;
21+
boolean eof = false;
22+
23+
ReaderInputStream(Reader r) {
24+
rd = r;
25+
bufferSize = 100;
26+
}
27+
28+
ReaderInputStream(Reader r, int size) {
29+
rd = r;
30+
bufferSize = size;
31+
}
32+
33+
private void buffering() throws IOException {
34+
if ( eof ) return;
35+
int ch = rd.read();
36+
if ( ch < 0 ) {
37+
eof = true;
38+
return;
39+
}
40+
if ( ch >= 0xd800 && ch <= 0xdbff ) {
41+
if ( pair != null ) {
42+
ch = pair[0];
43+
byte[] bytes = CodePoint.utf8(ch);
44+
pac.write(bytes);
45+
}
46+
pair = new char[] {(char)ch,0};
47+
buffering();
48+
return;
49+
} else if ( ch >= 0xdc00 && ch <= 0xdfff ) {
50+
byte[] bytes;
51+
if ( pair != null ) {
52+
pair[1] = (char)ch;
53+
bytes = String.valueOf(pair).getBytes(StandardCharsets.UTF_8);
54+
} else {
55+
bytes = CodePoint.utf8(ch);
56+
}
57+
pac.write(bytes);
58+
} else {
59+
pair = null;
60+
byte[] bytes = CodePoint.utf8(ch);
61+
pac.write(bytes);
62+
}
63+
}
64+
65+
@Override
66+
public int read() throws IOException {
67+
while ( !eof && pac.size() < bufferSize ) {
68+
buffering();
69+
}
70+
return pac.read();
71+
}
72+
73+
@Override
74+
public void close() throws IOException {
75+
rd.close();
76+
}
77+
78+
/**
79+
* 正確な長さがわからない
80+
* @return
81+
* @throws IOException
82+
*/
83+
@Override
84+
public int available() throws IOException {
85+
return pac.size() + (rd.ready() ? 1 : 0);
86+
}
87+
88+
}

src/main/java/net/siisise/io/StreamFrontPacket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public StreamFrontPacket(InputStream in) {
2323
}
2424

2525
public StreamFrontPacket(Reader reader) {
26-
this(new ReaderInputStream(reader));
26+
this(new ReaderInputStream(reader, 30));
2727
}
2828

2929
@Override

0 commit comments

Comments
 (0)