Skip to main content

convert vector to array of bytes

3 replies [Last post]
chopficaro
Offline
Joined: 2010-05-01
Points: 0

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.

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chopficaro
Offline
Joined: 2010-05-01
Points: 0

ah yes thank you that was the solution

chopficaro
Offline
Joined: 2010-05-01
Points: 0

oops, my bad. can a mod move this or should i re post?

kleopatra
Offline
Joined: 2003-06-11
Points: 0

and this is related to Swing or AWT in that ... ? Or in other words: post your unrelated questions in a more appropriate forum.

Jeanette