Jump to content

Wikipedia:WikiProject edit counters/Java Sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by AySz88 (talk | contribs) at 06:17, 16 January 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

public class ContribFileTester
{
	public static void main(String[] args)
	{
		try
		{
			parse(new URL("http://en.wikipedia.org/"));
		}
		catch (MalformedURLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void parse(URL location)
	{
		try
		{
			//System.out.println(location.openConnection().getContent());
			int length = location.openConnection().getContentLength();
			int loaded = 0;
			long allowedIters = 1000000;
			String debug = "Length: " + length;
			String content = debug + " CONTENT START: ";
			
			BufferedInputStream buffer = new BufferedInputStream(location.openStream());

			byte[] nextParcel = new byte[10000];
			int lengthRead = buffer.read(nextParcel);
			
			boolean end = false;
			while (!end)
			{
				while (lengthRead > 0)
				{
					content += new String(nextParcel);
					lengthRead = buffer.read(nextParcel);
					System.out.println(content.length());
				}
				byte testByte = (byte) buffer.read();
				if (testByte == -1)
					end = true;
				else
					content += (byte) testByte;
				System.out.println(testByte);
			}
				
			content += "-- CONTENT END";
			//System.out.println(content);				
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output

10026
20026
30026
40026
50026
60026
70026
80026
90026
100026
110026
-1

With content printed:

Blah blah blah blah blah blah blah blah.....then:

<!-- Served by srv2 in 0.07 secs. -->
</body></html>
a/en/3/35/Commons_without_text-35px.png" alt="commons:Main Page" width="35" height="46" longdesc="/wiki/Image:Commons_without_text-35px.png" /></a></div>
</div>
</td>
<td><a href="/wiki/Main_Page" class='extiw' title="commons:Main Page"><b>Commons</b></a><br />
Shared media repository</td>
<td>
<div style="position: relative; width: 35px; height: 33px; overflow: hidden">
<div style="position: absolute; top: 0px; left: 0px; font-size: 100px; overflow: hidden; line-height: 100px; z-index: 3"><a href="/wiki/Main_Page" class='extiw' title="m:Main Page">   </a></div>
<div style="position: absolute; top: 0px; left: 0px; z-index: 2"><a href="/wiki/Image:Wikimedia_without_text-35px.png" class="image" title="m:Main Page"><img src="/media/wikipedia/en/3/37/Wikimedia_without_text-35px.png" alt="m:Main Page" width="35" height="33" longdesc="/wiki/Image:Wikimedia_without_text-35px.png" /></a></div>
</div>
</td>
<td><a href="/wiki/Main_Page" class='extiw' title="m:Main Page"><b>Meta-Wiki</b></a><br />
Wikimedia project coordination</td>
</tr>
</table>
<div style="clear:left"></div>
</div>
<div style="display:none;">
<p><small class="plainlinks"><a href="/wiki/Special:Allpages" title="Special:Allpages">All</a> <a href="/w/wiki.phtml?title=Special:Newpages&limit=500&offset=0" class='external text' title="http://en.wikipedia.org/w/wiki.phtml?title=Special:Newpages&limit=500&offset=0"><b>New articles</b></a>: <a href="/w/wiki.phtml?title=Special:Newpa-- CONTENT END