All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.io.Test Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.ajax4jsf.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

public class Test {
	static int ARRAY_LENGTH = 27;
	static int READ_OFF = 4;
	static int READ_LENGTH = 22;
	
	static boolean BUILD_STRING = false;
	static boolean OUT_STRING = true;
	
	static void testStreams() throws IOException {
		String s = "This is a senseless text to test streams.\n";
		for (int i = 0; i < 10; i++) s = s + s; //repeated 16 times 
		byte[] bytes = s.getBytes();
		
		FastBufferOutputStream output = new FastBufferOutputStream(16);
		//write it several times.
		for (int i = 0; i < 4; i++) output.write(bytes);
		//write it one more time by one byte
		for (int i = 0; i < bytes.length; i++) {
			output.write(bytes[i]);
		}
		FastBufferInputStream input = new FastBufferInputStream(output);
		StringBuffer sb = new StringBuffer();
		//use for reading unconvenient array length.
		byte[] bs = new byte[ARRAY_LENGTH];
		int l = 0;
		while((l = input.read(bs, READ_OFF, READ_LENGTH)) >= 0) {
			if(BUILD_STRING) sb.append(new String(bs, READ_OFF, l));
		}
		if(BUILD_STRING && OUT_STRING) {
			System.out.println(sb);
			System.out.println("Length=" + output.getLength());
		}
	}
	
	static void testStandardStreams() throws IOException {
		String s = "This is a senseless text to test streams.\n";
		for (int i = 0; i < 10; i++) s = s + s; //repeated 16 times 
		byte[] bytes = s.getBytes();
		
		ByteArrayOutputStream output = new ByteArrayOutputStream(16);
		//write it several times.
		for (int i = 0; i < 4; i++) output.write(bytes);
		//write it one more time by one byte
		for (int i = 0; i < bytes.length; i++) {
			output.write(bytes[i]);
		}
		ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
		StringBuffer sb = new StringBuffer();
		//use for reading unconvenient array length.
		byte[] bs = new byte[ARRAY_LENGTH];
		int l = 0;
		while((l = input.read(bs, READ_OFF, READ_LENGTH)) >= 0) {
			if(BUILD_STRING) sb.append(new String(bs, READ_OFF, l));
		}
		if(BUILD_STRING && OUT_STRING) System.out.println(sb);
	}


	static void testReaders() throws IOException {
		String s = "This is a senseless text to test readers.\n";
		for (int i = 0; i < 10; i++) s = s + s; //repeated 16 times 
		char[] bytes = s.toCharArray();
		
		FastBufferWriter output = new FastBufferWriter(16);
		//write it several times.
		for (int i = 0; i < 4; i++) output.write(bytes);
		//write it one more time by one byte
		for (int i = 0; i < bytes.length; i++) {
			output.write(bytes[i]);
		}
		FastBufferReader input = new FastBufferReader(output);
		StringBuffer sb = new StringBuffer();
		//use for reading unconvenient array length.
		char[] bs = new char[ARRAY_LENGTH];
		int l = 0;
		while((l = input.read(bs, READ_OFF, READ_LENGTH)) >= 0) {
			if(BUILD_STRING) sb.append(new String(bs, READ_OFF, l));
		}
		if(BUILD_STRING && OUT_STRING) System.out.println(sb);
	}

	static void testStandardReaders() throws IOException {
		String s = "This is a senseless text to test readers.\n";
		for (int i = 0; i < 10; i++) s = s + s; //repeated 16 times 
		char[] bytes = s.toCharArray();
		
		StringWriter output = new StringWriter(16);
		//write it several times.
		for (int i = 0; i < 4; i++) output.write(bytes);
		//write it one more time by one byte
		for (int i = 0; i < bytes.length; i++) {
			output.write(bytes[i]);
		}
		StringReader input = new StringReader(output.toString());
		StringBuffer sb = new StringBuffer();
		//use for reading unconvenient array length.
		char[] bs = new char[ARRAY_LENGTH];
		int l = 0;
		while((l = input.read(bs, READ_OFF, READ_LENGTH)) >= 0) {
			if(BUILD_STRING) sb.append(new String(bs, READ_OFF, l));
		}
		if(BUILD_STRING && OUT_STRING) System.out.println(sb);
	}

	static void testTransitionFromWriterToStream() throws IOException {
		String s = "This is a senseless text to test transform from writer to stream.\n";
		for (int i = 0; i < 10; i++) s = s + s; //repeated 16 times 
		char[] bytes = s.toCharArray();
		
		FastBufferWriter output = new FastBufferWriter(16);
		//write it several times.
		for (int i = 0; i < 4; i++) output.write(bytes);
		//write it one more time by one byte
		for (int i = 0; i < bytes.length; i++) {
			output.write(bytes[i]);
		}
		FastBufferOutputStream output2 = output.convertToOutputStream("UTF-8");
		FastBufferInputStream input = new FastBufferInputStream(output2);
		StringBuffer sb = new StringBuffer();
		//use for reading unconvenient array length.
		byte[] bs = new byte[ARRAY_LENGTH];
		int l = 0;
		while((l = input.read(bs, READ_OFF, READ_LENGTH)) >= 0) {
			if(BUILD_STRING) sb.append(new String(bs, READ_OFF, l));
		}
		if(BUILD_STRING && OUT_STRING) System.out.println(sb);
	}

	static void testStandardTransitionFromWriterToStream() throws IOException {
		String s = "This is a senseless text to test transform from writer to stream.\n";
		for (int i = 0; i < 10; i++) s = s + s; //repeated 16 times 
		char[] bytes = s.toCharArray();
		
		StringWriter output = new StringWriter(16);
		//write it several times.
		for (int i = 0; i < 4; i++) output.write(bytes);
		//write it one more time by one byte
		for (int i = 0; i < bytes.length; i++) {
			output.write(bytes[i]);
		}
		String str = output.toString();
		ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes());
		StringBuffer sb = new StringBuffer();
		//use for reading unconvenient array length.
		byte[] bs = new byte[ARRAY_LENGTH];
		int l = 0;
		while((l = input.read(bs, READ_OFF, READ_LENGTH)) >= 0) {
			if(BUILD_STRING) sb.append(new String(bs, READ_OFF, l));
		}
		if(BUILD_STRING && OUT_STRING) System.out.println(sb);
	}

	static void testTransitionFromStreamToWriter() throws IOException {
		String s = "This is a senseless text to test transform from stream to writer.\n";
		for (int i = 0; i < 10; i++) s = s + s; //repeated 16 times 
		byte[] bytes = s.getBytes();
		
		FastBufferOutputStream output = new FastBufferOutputStream(16);
		//write it several times.
		for (int i = 0; i < 4; i++) output.write(bytes);
		//write it one more time by one byte
		for (int i = 0; i < bytes.length; i++) {
			output.write(bytes[i]);
		}
		FastBufferWriter output2 = output.convertToWriter("UTF-8");
		FastBufferReader input = new FastBufferReader(output2);
		StringBuffer sb = new StringBuffer();
		//use for reading unconvenient array length.
		char[] bs = new char[ARRAY_LENGTH];
		int l = 0;
		while((l = input.read(bs, READ_OFF, READ_LENGTH)) >= 0) {
			if(BUILD_STRING) sb.append(new String(bs, READ_OFF, l));
		}
		if(BUILD_STRING && OUT_STRING) System.out.println(sb);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long t = System.currentTimeMillis();
		try {
			for (int i = 0; i < 10; i++) {
//			testStreams();
//			testStandardStreams();

//			testReaders();
//				testStandardReaders();
				
//			testTransitionFromWriterToStream();
			testStandardTransitionFromWriterToStream();
			
//			testTransitionFromStreamToWriter();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		long dt = System.currentTimeMillis() - t;
		System.out.println(dt);

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy