Kompletny kod na zamianę Stringa na listę BigIntegerów i z powrotem:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
byte[] oryginał = "Ala ma kota. Kot ma AIDS. Żażółć gęślą jaźń.".getBytes("UTF-8");
ByteBuffer bb = ByteBuffer.wrap(oryginał);
List<BigInteger> biList = new ArrayList<BigInteger>();
byte[] bufor = new byte[5];
biList.add(oryginał.length % bufor.length == 0 ? BigInteger.ZERO : BigInteger.ONE);
while (bb.remaining() >= bufor.length) {
bb.get(bufor);
biList.add(new BigInteger(bufor));
}
if (bb.hasRemaining()) {
System.out.println("Niepodzielna wielkość.");
int length = bb.remaining();
Arrays.fill(bufor, 0, bufor.length - length - 1, (byte) 0);
bufor[bufor.length - length - 1] = 100;
bb.get(bufor, bufor.length - length, length);
biList.add(new BigInteger(bufor));
}
for (BigInteger bi : biList) {
System.out.println(bi.toString());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean uneven = biList.get(0).equals(BigInteger.ONE);
BigInteger lastBi = null;
for (BigInteger bi : biList.subList(1, biList.size())) {
if (lastBi != null) {
Arrays.fill(bufor, (byte) 0);
byte[] lastBiBytes = lastBi.toByteArray();
System.arraycopy(lastBiBytes, 0, bufor, bufor.length - lastBiBytes.length, lastBiBytes.length);
baos.write(bufor);
}
lastBi = bi;
}
if (uneven) {
bufor = lastBi.toByteArray();
baos.write(bufor, 1, bufor.length - 1);
} else {
baos.write(lastBi.toByteArray());
}
System.out.println(new String(baos.toByteArray(), "UTF-8"));
}
}
Edit:
Uwaga, kod jeszcze nie jest poprawny, a to dlatego, że BigInteger wyrzuca początkowe zera, więc jak będą zera w tekście (nie wiem nawet czy UTF-8 to dopuszcza) to się popsuje.
import java.math.BigInteger;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
byte[] tablica = new byte[]{0, 0, 1, 2, 3};
System.out.println(Arrays.toString(tablica));
BigInteger bi = new BigInteger(tablica);
tablica = bi.toByteArray();
System.out.println(Arrays.toString(tablica));
}
}
Update:
OK, kod już poprawiony. Zakładając, że z konwersji Stringa nie powstanie bajt o wartości 0 (rozsądne założenie) to kod mógłby być sporo uproszczony.
Update:
Uproszczony kod (z powyższym założeniem):
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
byte[] oryginał = "Ala ma kota. Kot ma AIDS. Żażółć gęślą jaźń.".getBytes("UTF-8");
ByteBuffer bb = ByteBuffer.wrap(oryginał);
List<BigInteger> biList = new ArrayList<BigInteger>();
final int chunkLength = 5;
while (bb.remaining() >= chunkLength) {
byte[] bufor = new byte[chunkLength];
bb.get(bufor);
biList.add(new BigInteger(bufor));
}
if (bb.hasRemaining()) {
System.out.println("Niepodzielna wielkość.");
byte[] bufor = new byte[bb.remaining()];
bb.get(bufor);
biList.add(new BigInteger(bufor));
}
for (BigInteger bi : biList) {
System.out.println(bi.toString());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (BigInteger bi : biList) {
baos.write(bi.toByteArray());
}
System.out.println(new String(baos.toByteArray(), "UTF-8"));
}
}