001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: TestSource.java$
021: * $FileID: 4500$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:20 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.server.source;
032:
033: /**
034: * Title: SourceJammer
035: * Description: Server code for SourceJammer Java open-source source control project.
036: * Copyright: Copyright (c) 2001
037: * Company: Robert MacGrogan
038: * @author Robert MacGrogan
039: * @version 1.0
040: */
041:
042: import org.sourcejammer.server.make.*;
043: import java.io.*;
044: import java.util.zip.*;
045: import org.sourcejammer.util.AppConfig;
046:
047: public class TestSource {
048:
049: public static void main(String[] args) {
050: /*
051: try {
052:
053: BinaryDelta oSource = new BinaryDelta();
054:
055: ReadBytes oRead1 = new ReadBytes();
056: oRead1.setNumBytesToRead(5);
057: oSource.addBuildStep(oRead1);
058:
059: AddBytes oAdd1 = makeAddBytes("c:\\Files\\test\\build\\add1.txt");
060: oSource.addBuildStep(oAdd1);
061:
062: ReadBytes oRead2 = new ReadBytes();
063: oRead2.setNumBytesToRead(7);
064: oSource.addBuildStep(oRead2);
065:
066: SkipBytes oSkip1 = new SkipBytes();
067: oSkip1.setNumBytesToSkip(2);
068: oSource.addBuildStep(oSkip1);
069:
070: AddBytes oAdd2 = makeAddBytes("c:\\Files\\test\\build\\add2.txt");
071: oSource.addBuildStep(oAdd2);
072:
073: ReadBytes oRead3 = new ReadBytes();
074: oRead3.setNumBytesToRead(3);
075: oSource.addBuildStep(oRead3);
076:
077: AddBytes oAdd3 = makeAddBytes("c:\\Files\\test\\build\\add3.txt");
078: oSource.addBuildStep(oAdd3);
079:
080: ReadBytes oRead4 = new ReadBytes();
081: oRead4.setNumBytesToRead(11);
082: oSource.addBuildStep(oRead4);
083:
084:
085: FileInputStream oFileInOld = new FileInputStream("c:\\Files\\test\\build\\old.txt");
086: ByteArrayOutputStream oByOutOld = new ByteArrayOutputStream();
087: byte[] byOld;
088: try {
089: boolean bKeepReadingBytes = true;
090: while (bKeepReadingBytes){
091: int iByte = oFileInOld.read();
092: if (iByte == -1){
093: bKeepReadingBytes = false;
094: }
095: else {
096: oByOutOld.write(iByte);
097: }
098: }
099: byOld = oByOutOld.toByteArray();
100: }
101: finally {
102: oByOutOld.close();
103: oFileInOld.close();
104: }
105:
106: SourceInputStream oOldSource = new SourceInputStream(byOld);
107: byte[] byResult = oSource.buildFile(oOldSource);
108:
109: FileOutputStream oFileOutResult = new FileOutputStream("c:\\Files\\test\\build\\result.txt");
110: try {
111: oFileOutResult.write(byResult);
112: }
113: finally {
114: oFileOutResult.close();
115: }
116: }
117: catch (Exception ex){
118: ex.printStackTrace();
119: }
120:
121: */
122:
123: /*
124:
125: byte[] byOld = new byte[50];
126: byte[] byNew = new byte[50];
127:
128: byte byDatum = 0;
129: for (int i = 0; i < 50; i++){
130: byOld[i] = byDatum;
131: byNew[i] = byDatum;
132: if(byDatum == 7){
133: byDatum = 0;
134: }
135: else {
136: byDatum++;
137: }
138: }
139: System.out.println("Got data. Let's compare.");
140:
141: ComparisonByteChunk oCompareOld = new ComparisonByteChunk(byOld, 0);
142: ComparisonByteChunk oCompareNew = new ComparisonByteChunk(byNew, 0);
143: if (oCompareNew.equals(oCompareOld)){
144: System.out.println("Equal!");
145: }
146: else {
147: System.out.println("NOT Equal!");
148: }
149: */
150: try {
151: int iZipBufferSize = 50;
152:
153: String sOldFile = args[0];
154: String sNewFile = args[1];
155: String sResultFile = args[2];
156: String sDiffFile = args[2] + ".diff";
157:
158: boolean bKeepReadingBytes = true;
159:
160: byte[] byOld;
161: FileInputStream oFileInOld = new FileInputStream(sOldFile);
162: ByteArrayOutputStream oByOutOld = new ByteArrayOutputStream();
163: try {
164: while (bKeepReadingBytes) {
165: int iByte = oFileInOld.read();
166: if (iByte == -1) {
167: bKeepReadingBytes = false;
168: } else {
169: oByOutOld.write(iByte);
170: }
171: }
172: byOld = oByOutOld.toByteArray();
173: } finally {
174: oByOutOld.close();
175: oFileInOld.close();
176: }
177:
178: /*
179: //deflate old file
180: Deflater oDeflate = new Deflater(Deflater.DEFLATED, true);
181: oDeflate.setInput(byOld);
182: oDeflate.finish();
183: ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
184: while (! oDeflate.finished() ){
185: byte[] byRead = new byte[iZipBufferSize];
186: int iBytesRead = oDeflate.deflate(byRead);
187: if (iBytesRead == byRead.length){
188: oZipStream.write(byRead);
189: }
190: else {
191: oZipStream.write(byRead, 0, iBytesRead);
192: }
193: }
194: oDeflate.end();
195: byte[] byOldZip = oZipStream.toByteArray();
196: oZipStream.close();
197: */
198:
199: bKeepReadingBytes = true;
200: byte[] byNew;
201: FileInputStream oFileInNew = new FileInputStream(sNewFile);
202: ByteArrayOutputStream oByOutNew = new ByteArrayOutputStream();
203: try {
204: while (bKeepReadingBytes) {
205: int iByte = oFileInNew.read();
206: if (iByte == -1) {
207: bKeepReadingBytes = false;
208: } else {
209: oByOutNew.write(iByte);
210: }
211: }
212: byNew = oByOutNew.toByteArray();
213: } finally {
214: oByOutNew.close();
215: oFileInNew.close();
216: }
217:
218: /*
219: //deflate new file
220: oDeflate = new Deflater(Deflater.DEFLATED, true);
221: oDeflate.setInput(byNew);
222: oDeflate.finish();
223: oZipStream = new ByteArrayOutputStream();
224: while (! oDeflate.finished() ){
225: byte[] byRead = new byte[iZipBufferSize];
226: int iBytesRead = oDeflate.deflate(byRead);
227: if (iBytesRead == byRead.length){
228: oZipStream.write(byRead);
229: }
230: else {
231: oZipStream.write(byRead, 0, iBytesRead);
232: }
233: }
234: oDeflate.end();
235: byte[] byNewZip = oZipStream.toByteArray();
236: oZipStream.close();
237: */
238:
239: System.out.println("Old size: " + byOld.length);
240: System.out.println("New size: " + byNew.length);
241:
242: /*
243: System.out.println("Old xip size: " + byOldZip.length);
244: System.out.println("New zip size: " + byNewZip.length);
245: */
246:
247: long lBeginTime = new java.util.Date().getTime();
248:
249: BuildDelta oDeltaMaker = new BuildDelta();
250: System.out.println("About to build delta.");
251:
252: BinaryDelta oDiff = oDeltaMaker.buildBinaryDelta(byOld,
253: byNew);
254: /*
255: TextDelta oDiff = oDeltaMaker.buildTextDelta(byOld, byNew);
256: */
257: /*
258: MakeSourceVersion makeVer = new MakeSourceVersion();
259: makeVer.setNewSource(byNew);
260: makeVer.setOldSource(byOld);
261: BinaryDelta oDiff = makeVer.makeNewVersionFile();
262: */
263: long lEndTime = new java.util.Date().getTime();
264: long lElapsed = lEndTime - lBeginTime;
265: System.out.println("Millis to make: " + lElapsed);
266:
267: ByteArrayOutputStream oDiffZipBytes = new ByteArrayOutputStream();
268: ObjectOutputStream oObjOutDiff = new ObjectOutputStream(
269: oDiffZipBytes);
270: oObjOutDiff.writeObject(oDiff);
271: oObjOutDiff.flush();
272:
273: FileOutputStream oFileOutDiff = new FileOutputStream(
274: sDiffFile);
275: try {
276: DeflaterOutputStream oDeflateStream = new DeflaterOutputStream(
277: oFileOutDiff);
278:
279: byte[] byDiff = oDiffZipBytes.toByteArray();
280:
281: for (int iCounter = 0; iCounter < byDiff.length; iCounter++) {
282: oDeflateStream.write(byDiff[iCounter]);
283: }
284: oDeflateStream.finish();
285: oDeflateStream.close();
286: } finally {
287: oFileOutDiff.close();
288: }
289:
290: SourceInputStream oOldSource = new SourceInputStream(byOld);
291: oOldSource.setStreamType(AppConfig.FileTypes.BINARY);
292: ByteArrayOutputStream oOutStrResult = oDiff
293: .buildFile(oOldSource);
294:
295: /*
296: //inflate the result file
297: Inflater oInflate = new Inflater(true);
298: oInflate.setInput(oOutStrResult.toByteArray());
299:
300: oZipStream = new ByteArrayOutputStream();
301: while (! oInflate.finished() ){
302: byte[] byRead = new byte[iZipBufferSize];
303: int iBytesRead = oInflate.inflate(byRead);
304: if (iBytesRead == byRead.length){
305: oZipStream.write(byRead);
306: }
307: else {
308: oZipStream.write(byRead, 0, iBytesRead);
309: }
310: }
311: oDeflate.end();
312: */
313:
314: FileOutputStream oFileOutResult = new FileOutputStream(
315: sResultFile);
316:
317: try {
318: oOutStrResult.writeTo(oFileOutResult);
319: } finally {
320: oFileOutResult.close();
321: oOutStrResult.close();
322: }
323: } catch (Exception ex) {
324: ex.printStackTrace();
325: }
326:
327: /* boolean bFirst = true;
328:
329:
330: byte[] test1 = new byte[40];
331: for (int i = 0; i<=19; i++){
332: byte byData = 0;
333: if (bFirst){
334: byData = 1;
335: }
336: else {
337: byData = 2;
338: }
339: test1[i] = byData;
340: bFirst = !bFirst;
341: }
342: for (int i = 20; i<=39; i++){
343: byte byData = 0;
344: if (bFirst){
345: byData = 3;
346: }
347: else {
348: byData = 4;
349: }
350: test1[i] = byData;
351: bFirst = !bFirst;
352: }
353:
354: byte [] test2 = new byte[10];
355: for (int i = 0; i<=9; i++){
356: test2[i] = 5;
357: }
358:
359: BinaryDelta oSource = new BinaryDelta();
360:
361: ReadBytes step1 = new ReadBytes();
362: step1.setNumBytesToRead(10);
363: oSource.addBuildStep(step1);
364:
365: AddBytes step2 = new AddBytes();
366: step2.setBytesToAdd(test2);
367: oSource.addBuildStep(step2);
368:
369: SkipBytes step3 = new SkipBytes();
370: step3.setNumBytesToSkip(10);
371: oSource.addBuildStep(step3);
372:
373: ReadBytes step4 = new ReadBytes();
374: step4.setNumBytesToRead(20);
375: oSource.addBuildStep(step4);
376:
377: AddBytes step5 = new AddBytes();
378: step5.setBytesToAdd(test2);
379: oSource.addBuildStep(step5);
380:
381: System.out.println("Source:");
382: for (int i = 0; i < test1.length; i++){
383: System.out.print(test1[i]);
384: }
385: System.out.print("\r\n");
386:
387: SourceInputStream source = new SourceInputStream(test1);
388: try {
389: byte[] buildBytes = oSource.buildFile(source);
390:
391: for (int i = 0; i < buildBytes.length; i++){
392: System.out.print(buildBytes[i]);
393: }
394: }
395: catch (InvalidSourceException ex){
396: ex.printStackTrace();
397: }
398: */
399:
400: }
401:
402: private static AddBytes makeAddBytes(String sFileName)
403: throws IOException {
404:
405: AddBytes oReturn = new AddBytes();
406: FileInputStream oFileInAdd1 = new FileInputStream(sFileName);
407: ByteArrayOutputStream oByOutAdd1 = new ByteArrayOutputStream();
408: try {
409: boolean bKeepReadingBytes = true;
410: while (bKeepReadingBytes) {
411: int iByte = oFileInAdd1.read();
412: if (iByte == -1) {
413: bKeepReadingBytes = false;
414: } else {
415: oByOutAdd1.write(iByte);
416: }
417: }
418: byte[] byAdd1 = oByOutAdd1.toByteArray();
419: oReturn.setBytesToAdd(byAdd1);
420: } finally {
421: oByOutAdd1.close();
422: oFileInAdd1.close();
423: }
424:
425: return oReturn;
426: }
427: }
|