19 ต.ค. 2554

TestReadSummary

package ball.summary;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JOptionPane;

import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;

public class ReadSummary {
 final static String FILENAME = "2.pdf:\5SummaryInformation";

 protected String fileName;

 protected RandomAccessFile seeker;

public static void main(String[] args) throws IOException, NoPropertySetStreamException, MarkUnsupportedException {
// ReadSummary read = new ReadSummary();
// read.RandomRead(FILENAME);
//    System.out.println("Offset is " + read.readOffset());
//    System.out.println("Message is \"" + read.readMessage() + "\".");
BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream("2.pdf:\5SummaryInformation")));

// byte[] getByte = getBytesFromFile(new File("2.pdf:\5SummaryInformation"));
String str = "";
String a = "";
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> resultByte = new ArrayList<String>();
while((a=buf.readLine()) != null){
str+=a;
}
String buffer = str;
String[] splitStringArray = buffer.split("P");

int offset = 0;
for(String s: splitStringArray) {
   offset = buffer.indexOf(s);
   System.out.println(">"+offset);
}
System.out.println(new String(str));
System.exit(0);
char[] ch = null;
if(!str.trim().equals("")){
ch = str.toCharArray();
StringBuffer bufByte = new StringBuffer();
String com = "0/0/30/0/0/0/";
String zero = "0/0/0/0/0/0/0/0/0/";
for (int i = 0; i < ch.length; i++) {
int numb = (byte)ch[i];
//System.out.println(numb+" = "+ch[i]);
bufByte.append(numb+"/");
String strbuf = bufByte.toString();
// if(ch[i]=='A')
// ch[i]='X';
resultByte.add(String.valueOf(ch[i]));
if(strbuf.indexOf(com)>=0){
//System.out.println("BUFFFF "+strbuf);
String allstrbuf = strbuf.substring(4,strbuf.indexOf(com)-1);
String[] splitstrbuf = allstrbuf.split("/");
byte[] byteArray = new byte[splitstrbuf.length];
for (int j = 0; j < splitstrbuf.length; j++) {
if(!splitstrbuf[j].equals(""))
byteArray[j] = (byte) Integer.parseInt(splitstrbuf[j]);
}
String byteresult = new String(byteArray).trim();
if(!byteresult.equals(""))
result.add(byteresult);
bufByte = new StringBuffer();
}
if(strbuf.indexOf(zero)>=0){
//System.out.println(strbuf);
String allstrbuf = strbuf.substring(0,strbuf.indexOf(zero));
//System.out.println(allstrbuf);
String[] splitstrbuf = allstrbuf.split("/");
byte[] byteArray = new byte[splitstrbuf.length];
for (int j = 0; j < splitstrbuf.length; j++) {
if(!splitstrbuf[j].equals(""))
byteArray[j] = (byte) Integer.parseInt(splitstrbuf[j]);
}
String byteresult = new String(byteArray).trim();
if(!byteresult.equals(""))
result.add(byteresult);
bufByte = new StringBuffer();
}
}
}
for (String arr : result) {
// System.out.println("Result "+arr);
}
String allResult = "";
for (String arr : resultByte) {
allResult+=arr;
}
OutputStreamWriter bw = new OutputStreamWriter(new FileOutputStream("ByteResult.txt", true));
bw.write(str);
bw.close();
System.out.println("Done");
}

 /** Constructor: save filename, construct RandomAccessFile */
 public void RandomRead(String fname) throws IOException {
   fileName = fname;
   seeker = new RandomAccessFile(fname, "r");
 }

 /** Read the Offset field, defined to be at location 0 in the file. */
 public int readOffset() throws IOException {
   seeker.seek(0); // move to very beginning
   return seeker.readInt(); // and read the offset
 }

 /** Read the message at the given offset */
 public String readMessage() throws IOException {
   seeker.seek(readOffset()); // move to the offset
   return seeker.readLine(); // and read the String
 }
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}

// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}

// Close the input stream and return bytes
is.close();
return bytes;
}

private static void copyfile(String srFile, String dtFile) {
try {
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);

// For Append the file.
// OutputStream out = new FileOutputStream(f2,true);

// For Overwrite the file.
OutputStream out = new FileOutputStream(f2);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
} catch (FileNotFoundException ex) {
System.out
.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}