Note that there are some explanatory texts on larger screens.

plurals
  1. POIs There a More Efficient Way to Convert Between ArrayList and Array
    text
    copied!<p>Using Java, I have a class which retrieves a webpage as a byte array. I then need to strip out some content if it exists. (The application monitors web pages for changes, but needs to remove session Ids from the html which are created by php, and would mean changes were detected each visit to the page).</p> <p>Some of the resulting byte arrays could be 10s of 1000s bytes long. They're not stored like this - a 16 byte MD5 of the page is stored. However, it is the original full size byte array which needs to be processed.</p> <p>(<strong>UPDATE - the code does not work. See comment from A.H. below</strong>) A test showing my code:</p> <pre><code>public void testSessionIDGetsRemovedFromData() throws IOException { byte[] forumContent = "&lt;li class=\"icon-logout\"&gt;&lt;a href=\"./ucp.php?mode=logout&amp;amp;sid=3a4043284674572e35881e022c68fcd8\" title=\"Logout [ barry ]\" accesskey=\"x\"&gt;Logout [ barry ]&lt;/a&gt;&lt;/li&gt;".getBytes(); byte[] sidPattern = "&amp;amp;sid=".getBytes(); int sidIndex = ArrayCleaner.getPatternIndex(forumContent, sidPattern); assertEquals(54, sidIndex); // start of cleaning code ArrayList&lt;Byte&gt; forumContentList = new ArrayList&lt;Byte&gt;(); forumContentList.addAll(forumContent); forumContentList.removeAll(Arrays.asList(sidPattern)); byte[] forumContentCleaned = new byte[forumContentList.size()]; for (int i = 0; i &lt; forumContentCleaned.length; i++) { forumContentCleaned[i] = (byte)forumContentList.get(i); } //end of cleaning code sidIndex = ArrayCleaner.getPatternIndex(forumContentCleaned, sidPattern); assertEquals(-1, sidIndex); } </code></pre> <p>This all works fine, but I'm worried about the efficiency of the cleaning section. I had hoped to operate solely on arrays, but the ArrayList has nice built in functions to removed a collection from the ArrayList, etc, which is just what I need. So I have had to create an ArrayList of Byte, as I can't have an ArrayList of the primitive byte (can anyone tell me why?), convert the pattern to remove to another ArrayList (I suppose this could be an ArrayList all along) to pass to removeAll(). I then need to create another byte[] and cast each element of the ArrayList of Bytes to a byte and add it to the byte[].</p> <p>Is there a more efficient way of doing all this? Can it be performed using arrays?</p> <p><em><strong>UPDATE</em></strong> This is the same functionality using strings:</p> <pre><code> public void testSessionIDGetsRemovedFromDataUsingStrings() throws IOException { String forumContent = "&lt;li class=\"icon-logout\"&gt;&lt;a href=\"./ucp.php?mode=logout&amp;amp;sid=3a4043284674572e35881e022c68fcd8\" title=\"Logout [ barry ]\" accesskey=\"x\"&gt;Logout [ barry ]&lt;/a&gt;&lt;/li&gt;"; String sidPattern = "&amp;amp;sid="; int sidIndex = forumContent.indexOf(sidPattern); assertEquals(54, sidIndex); forumContent = forumContent.replaceAll(sidPattern, ""); sidIndex = forumContent.indexOf(sidPattern); assertEquals(-1, sidIndex); } </code></pre> <p>Is this as efficient as the array/arrayList method?</p> <p>Thanks, Barry</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload