<commit_before>/* This file is part of the Joshua Machine Translation System.
 * 
 * Joshua is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */
package joshua.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;


/**
 * 
 * @author <NAME>, <<EMAIL>>
 * @version $LastChangedDate$
 */
public class SocketUtility {
		
	//############# client side #########
	//connect to server
	public static ClientConnection open_connection_client(String hostname, int port){
		ClientConnection res = new ClientConnection();
		res.hostname=hostname;
		res.port=port;
	    try {
	            InetAddress addr = InetAddress.getByName(hostname);
	            SocketAddress sockaddr = new InetSocketAddress(addr, port);
	          
	            res.socket = new Socket();  // Create an unbound socket
	            // This method will block no more than timeoutMs If the timeout occurs, SocketTimeoutException is thrown.
	            int timeoutMs = 3000;   // 2 seconds
	            res.socket.connect(sockaddr, timeoutMs);
	            res.socket.setKeepAlive( true ); 
	            //file
	            res.in = new BufferedReader( new InputStreamReader(res.socket.getInputStream()));
	            res.out = new PrintWriter( new OutputStreamWriter(res.socket.getOutputStream()));
//	            res.data_in = new DataInputStream( new BufferedInputStream( res.socket.getInputStream())) ;
//	            res.data_out = new DataOutputStream( new BufferedOutputStream (res.socket.getOutputStream()));


		} catch (UnknownHostException e) {
			System.out.println("unknown host exception");
			System.exit(1);
		} catch (SocketTimeoutException e) {
			System.out.println("socket timeout exception");
			System.exit(1);
		} catch (IOException e) {
			System.out.println("io exception");
			System.exit(1);
		}
		return res;
	}
	
	
	public static class ClientConnection{
		String hostname;//server name
		int port;//server port
		Socket socket;
		public BufferedReader in;
	    public PrintWriter out;
        //public DataOutputStream data_out; //debug
        //public DataInputStream data_in; //debug

	    
	    public String exe_request(String line_out){
	    	String line_res=null;
	    	try {
	    		out.println(line_out);
	            out.flush();
	            line_res = in.readLine(); //TODO block function, big bug, the server may close the section (e.g., the server thread is dead due to out of memory(which is possible due to cache) )
	        } catch(IOException ioe) {
	            ioe.printStackTrace();
	        }
	        return line_res;
	    }
	    
	    public void write_line(String line_out){	    	
	    	out.println(line_out);
	        out.flush();
	    }
	    
	    public void write_int(int line_out){	    	
	    	out.println(line_out);
	        out.flush();
	    }
	    
	    public String read_line(){
	    	String line_res=null;
	    	try {	    		
	            line_res = in.readLine(); //TODO block function, big bug, the server may close the section (e.g., the server thread is dead due to out of memory(which is possible due to cache) )
	        } catch(IOException ioe) {
	            ioe.printStackTrace();
	        }
	        return line_res;
	    }
	    
	    
	    public  void close(){
	    	try {
	    		socket.close();
	        } catch(IOException ioe) {
	            ioe.printStackTrace();
	        }	    	
	    }

	   public static double readDoubleLittleEndian( DataInputStream d_in){
		   long accum = 0;
	    	try {	    		

		   for ( int shiftBy=0; shiftBy<64; shiftBy+=8 ){
		      // must cast to long or shift done modulo 32
		      accum |= ( (long)( d_in.readByte() & 0xff ) ) << shiftBy;
	      	   }
                } catch(IOException ioe) {
	            ioe.printStackTrace();
	        }

		   return Double.longBitsToDouble( accum );	
	   // there is no such method as Double.reverseBytes( d );
          }

   }
	
}
<commit_msg>joshua.util.SocketUtility: cleaned up formatting and commented out unused fields (in preparation for removal)<commit_after>/* This file is part of the Joshua Machine Translation System.
 * 
 * Joshua is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */
package joshua.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;


/**
 * 
 * @author <NAME>, <<EMAIL>>
 * @version $LastChangedDate$
 */
public class SocketUtility {
		
	//############# client side #########
	//connect to server
	public static ClientConnection
	open_connection_client(String hostname, int port) {
		ClientConnection res = new ClientConnection();
		// TODO: remove from class
		// res.hostname = hostname;
		// res.port = port;
		try {
			InetAddress addr = InetAddress.getByName(hostname);
			SocketAddress sockaddr = new InetSocketAddress(addr, port);
			
			res.socket = new Socket(); // Create an unbound socket
			// This method will block no more than timeoutMs If the timeout occurs, SocketTimeoutException is thrown.
			int timeoutMs = 3000; // 2 seconds
			res.socket.connect(sockaddr, timeoutMs);
			res.socket.setKeepAlive(true);
			//file
			res.in = new BufferedReader(
				new InputStreamReader(res.socket.getInputStream()));
			res.out = new PrintWriter(
				new OutputStreamWriter(res.socket.getOutputStream()));
			
			// TODO: for debugging, but should be removed
			//res.data_in = new DataInputStream(new BufferedInputStream( res.socket.getInputStream()));
			//res.data_out = new DataOutputStream(new BufferedOutputStream (res.socket.getOutputStream()));
			
		} catch (UnknownHostException e) {
			System.out.println("unknown host exception");
			System.exit(1);
		} catch (SocketTimeoutException e) {
			System.out.println("socket timeout exception");
			System.exit(1);
		} catch (IOException e) {
			System.out.println("io exception");
			System.exit(1);
		}
		return res;
	}
	
	
	public static class ClientConnection {
		// TODO: These are never read from, so we're planning to remove them
		// String hostname; // server name
		// int port; // server port
		Socket socket;
		public BufferedReader in;
		public PrintWriter out;
		
		// TODO: for debugging, but should be removed
		//public DataOutputStream data_out;
		//public DataInputStream data_in;
		
		public String exe_request(String line_out) {
			String line_res = null;
			try {
				out.println(line_out);
				out.flush();
				line_res = in.readLine(); //TODO block function, big bug, the server may close the section (e.g., the server thread is dead due to out of memory(which is possible due to cache) )
			} catch(IOException ioe) {
				ioe.printStackTrace();
			}
			return line_res;
		}
		
		public void write_line(String line_out) {
			out.println(line_out);
			out.flush();
		}
		
		public void write_int(int line_out) {
			out.println(line_out);
			out.flush();
		}
		
		public String read_line() {
			String line_res = null;
			try {
				line_res = in.readLine(); //TODO block function, big bug, the server may close the section (e.g., the server thread is dead due to out of memory(which is possible due to cache) )
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			return line_res;
		}
		
		
		public void close() {
			try {
				socket.close();
			} catch(IOException ioe) {
				ioe.printStackTrace();
			}
		}
		
		public static double readDoubleLittleEndian(DataInputStream d_in) {
			long accum = 0;
			try {
				for (int shiftBy = 0; shiftBy < 64; shiftBy += 8) {
					// must cast to long or shift done modulo 32
					accum |= ((long)( d_in.readByte() & 0xff )) << shiftBy;
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			
			return Double.longBitsToDouble(accum);
			// there is no such method as Double.reverseBytes(d);
		}
	}
}

{'fraction_non_alphanumeric': 0.09984415584415585, 'fraction_numerical': 0.00664935064935065, 'mean_word_length': 2.948318293683347, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7229810', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2681, 'n_words': 915}