Projekt

Obecné

Profil

Stáhnout (2.08 KB) Statistiky
| Větev: | Tag: | Revize:
1
package io;
2

    
3
import java.io.BufferedInputStream;
4
import java.io.BufferedWriter;
5
import java.io.ByteArrayInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileWriter;
10
import java.io.IOException;
11
import java.io.PrintStream;
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.NoSuchElementException;
15

    
16
public class FileWorker {
17
	
18
	public static PrintStream createRedirectStream() throws FileNotFoundException {
19
		return new PrintStream(new File("jdeserialize_log.txt"));
20
	}
21
	
22
	public static byte[] loadByteArray(File file) throws IOException {
23
		byte fileContent[] = loadFileContent(file);
24
		byte buffer[];
25
		
26
		IZipReader input;
27
		try {
28
			input = new ZipInStream(new ByteArrayInputStream(fileContent));
29
		} catch (NoSuchElementException e) {
30
			System.out.println("Note: the file may be a special type of ZIP archive.");
31
			input = new InflaterInStream(new FileInputStream(file));
32
		}
33
		
34
		try {
35
			List<Byte> _buffer = new ArrayList<Byte>();
36
			while (true) {
37
				int value = input.read();
38
				if (value < 0) {
39
					break;
40
				} else {
41
					_buffer.add((byte) value);
42
				}
43
			}
44

    
45
			buffer = new byte[_buffer.size()];
46
			for (int i = 0; i < buffer.length; i++) {
47
				buffer[i] = _buffer.get(i);
48
			}
49
			
50
			System.out.println(String.format("The file is a%s ZIP archive...", input.getClass() == InflaterInStream.class ? " special" : ""));
51
		} catch (Exception e) {
52
			System.out.println("The file is not a ZIP archive...");
53
			buffer = fileContent;
54
		} finally {
55
			input.close();
56
		}
57

    
58
		System.out.println("Bytes from the file were loaded correctly.");
59
		return buffer;
60
	}
61

    
62
	private static byte[] loadFileContent(File file) throws IOException {
63
		byte buffer[] = new byte[(int) file.length()];
64
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
65
		bis.read(buffer);
66
		bis.close();
67
		return buffer;
68
	}
69
	
70
	public static void saveJson(File file, String json) throws IOException {
71
		FileWriter fw = new FileWriter(file);
72
		BufferedWriter bw = new BufferedWriter(fw);
73
		bw.write(json);
74
		bw.flush();
75
		bw.close();
76
	}
77
	
78
}
(1-1/4)