Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: convert vector to array of bytes

  1. #1
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default convert vector to array of bytes

    i had a program that could get almost any web address and save the file it returned, but it didnt work for php generated addresses. the reason was that my code depended on there being a contentLength attribute, and there is none generated for things like this (like a google search). so i used an if statement to test weather there was one, and if there wasnt, instead of using a buffered read, i tried to read the contents one byte at a time. i had to use a vector for this because again, the contentLength wasnt known. now im stuck with a vector and i dont know how to get it into the file.

    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.util.Vector;
     
    public class SourceViewer3
    {
    	public static void main (String[] args) throws Exception
    	{
    		for (int i = 0; i < 1; i++)//args.length; i++)
    		{
    			try
    			{
    				URL u = new URL("http://www.google.com/#hl=en&source=hp&q=unreal&aq=f&aqi=g10&aql=&oq=&gs_rfai=&fp=84c7fb41710deb10");
    				HttpURLConnection uc = (HttpURLConnection)u.openConnection( );
    				int code = uc.getResponseCode( );
    				String response = uc.getResponseMessage( );
    				System.out.println("HTTP/1.x " + code + " " + response);
    				for (int j = 1; ; j++)
    				{
    					String header = uc.getHeaderField(j);
    					String key = uc.getHeaderFieldKey(j);
    					if (header == null || key == null) break;
    					System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
    				}
    				String contentType = uc.getContentType();
    				int contentLength = uc.getContentLength();
    				System.out.println(contentType + contentLength);
    				InputStream in = new BufferedInputStream(uc.getInputStream( ));
    				if (contentLength == -1) 
    				{
    					System.out.println("Alert: No contentLength, must be php or something, proceeding with slower bit by bit read\n.");
    					Reader r = new InputStreamReader(in);
    					int c;
    					Vector<Object> data = new Vector<Object>();
    					contentLength = 0;
    					while ((c = r.read( )) != -1)
    					{
    						System.out.print((Object) c);
    						data.add(c);
    						contentLength++;
    					}
    					in.close();
    					String dirText = "text";
    					String dirTarget = "target";
    					String filename = "000" + u.getFile().substring(u.getFile().lastIndexOf('/') + 1);
    					String dirAndFile;
    					String dirNoSlashes = "run_from_main";
    					// determine weather the file is a target or is to be parsed for more files
    					if(contentType.startsWith("text/")) 
    					{
    						dirAndFile = dirNoSlashes + "\\" + dirText + "\\" + filename;
    					}
    					else 
    					{
    						dirAndFile = dirNoSlashes + "\\" + dirTarget + "\\" + filename;
    					}
    					FileOutputStream out = new FileOutputStream(dirAndFile);
    					byte[] data1 = (byte[])data.toArray();
    					out.write(data1);
    					out.flush();
    					out.close();
    				}
    				else
    				{
    					byte[] data = new byte[contentLength];
    					int bytesRead = 0;
    					int offset = 0;
    					while (offset < contentLength) 
    					{
    						bytesRead = in.read(data, offset, data.length - offset);
    						if (bytesRead == -1)
    						{
    							break;
    						}
    						offset += bytesRead;
    						System.out.println("bytes read ="+ bytesRead +"\n");
    					}
    					in.close();	
    					if (offset != contentLength) 
    					{
    						throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
    					}	
    					String dirText = "text";
    					String dirTarget = "target";
    					String filename = "000" + u.getFile().substring(u.getFile().lastIndexOf('/') + 1);
    					String dirAndFile;
    					String dirNoSlashes = "run_from_main";
    					// determine weather the file is a target or is to be parsed for more files
    					if(contentType.startsWith("text/")) 
    					{
    						dirAndFile = dirNoSlashes + "\\" + dirText + "\\" + filename;
    					}
    					else 
    					{
    						dirAndFile = dirNoSlashes + "\\" + dirTarget + "\\" + filename;
    					}
    					FileOutputStream out = new FileOutputStream(dirAndFile);
    					out.write(data);
    					out.flush();
    					out.close();
    				}
     
    			}
    			catch (MalformedURLException ex) 
    			{
    				System.err.println(args[0] + " is not a parseable URL");
    			}
    			catch (IOException ex) 
    			{
    				System.err.println(ex);
    			}
    		}
    	}
    }


  2. #2
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: convert vector to array of bytes

    i found a solution

Similar Threads

  1. How Convert a Byte array to a image format
    By perlWhite in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 19th, 2011, 03:16 PM
  2. [SOLVED] 2D Vector
    By nasi in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 01:42 AM
  3. Convert Vector to Byte Array
    By perlWhite in forum Java Theory & Questions
    Replies: 0
    Last Post: August 25th, 2009, 05:45 AM
  4. vector
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 8
    Last Post: August 12th, 2009, 10:17 AM
  5. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM