Jump to content
  • 0

Java.io, вопрос шарящим.


lolka84
 Share

Question

Привет!

Есть функция:






package ru.korshun.imgcrypto;

import java.io.*;

public class ImgCrypto {

public static void main(String[] args) {

ImgCrypto ic = new ImgCrypto();

try {
byte[] enc = ic.load();
String e = ic.encode(enc);

try (OutputStream out = new FileOutputStream("D:\\java\\ImgCrypto\\img\\image.jpg")) {
out.write(ic.decode(e));
}

} catch (IOException e) { }

}


public byte[] load() throws FileNotFoundException, IOException {

InputStream in = new FileInputStream("D:\\java\\ImgCrypto\\img\\20130826_103942.jpg");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[32 * 1024];

while (true) {
int r = in.read(buffer);
if (r > 0) { bout.write(buffer, 0, r); }
else { break; }
}

bout.close();

return bout.toByteArray();
}

//——————————————————-

public final String encode(byte[] d) {
if (d == null) return null;
byte data[] = new byte[d.length+2];
System.arraycopy(d, 0, data, 0, d.length);
byte dest[] = new byte[(data.length/3)*4];

// 3-byte to 4-byte conversion
for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4) {
dest[didx] = (byte) ((data[sidx] >>> 2) & 077);
dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 | (data[sidx] << 4) & 077);
dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 | (data[sidx+1] << 2) & 077);
dest[didx+3] = (byte) (data[sidx+2] & 077);
}

for (int idx = 0; idx < dest.length; idx++) {
if (dest[idx] < 26) { dest[idx] = (byte)(dest[idx] + 'A'); }
else if (dest[idx] < 52) { dest[idx] = (byte)(dest[idx] + 'a' - 26); }
else if (dest[idx] < 62) { dest[idx] = (byte)(dest[idx] + '0' - 52); }
else if (dest[idx] < 63) { dest[idx] = (byte)'+'; }
else { dest[idx] = (byte)'/'; }
}

for (int idx = dest.length-1; idx > (d.length*4)/3; idx--) { dest[idx] = (byte)'='; }

return new String(dest);

}

public final String encode(String s) { return encode(s.getBytes()); }


public final byte[] decode(String str) {
if (str == null) { return null; }
byte data[] = str.getBytes();
return decode(data);
}

public final static byte[] decode(byte[] data) {
int tail = data.length;
while (data[tail-1] == '=') { tail--; }
byte dest[] = new byte[tail - data.length/4];

for (int idx = 0; idx <data.length; idx++) {
if (data[idx] == '=') { data[idx] = 0; }
else if (data[idx] == '/') { data[idx] = 63; }
else if (data[idx] == '+') { data[idx] = 62; }
else if (data[idx] >= '0' && data[idx] <= '9') { data[idx] = (byte)(data[idx] - ('0' - 52)); }
else if (data[idx] >= 'a' && data[idx] <= 'z') { data[idx] = (byte)(data[idx] - ('a' - 26)); }
else if (data[idx] >= 'A' && data[idx] <= 'Z') { data[idx] = (byte)(data[idx] - 'A'); }
}

int sidx, didx;
for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3) {
dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx+1] >>> 4) & 3));
dest[didx+1] = (byte) (((data[sidx+1] << 4) & 255) | ((data[sidx+2] >>> 2) & 017));
dest[didx+2] = (byte) (((data[sidx+2] << 6) & 255) | (data[sidx+3] & 077));
}
if (didx < dest.length) { dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx+1] >>> 4) & 3)); }
if (++didx < dest.length) { dest[didx] = (byte) (((data[sidx+1] << 4) & 255) | ((data[sidx+2] >>> 2) & 017)); }

return dest;

}

}


Мне же нужно ее видоизменить для того, что бы можно было отдельно:

а) взять jpg, закодировать и сохранить на диск кодированный файл.

б) взять закодированный файл и получить jpg

 

Собственных знаний на это к сожалению пока не хватает :(

Очень надеюсь, что подскажут, как возможно реализовать мою задачу.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy