Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 195890 Details for
Bug 346104
[Performance] ResourceUtil read and write methods should use buffers
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Cumulative patch to improve ResourceUtil#write
clipboard.txt (text/plain), 19.47 KB, created by
Ralf Sternberg
on 2011-05-17 12:34:48 EDT
(
hide
)
Description:
Cumulative patch to improve ResourceUtil#write
Filename:
MIME Type:
Creator:
Ralf Sternberg
Created:
2011-05-17 12:34:48 EDT
Size:
19.47 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.rap.rwt >Index: src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator.java >=================================================================== >RCS file: /cvsroot/rt/org.eclipse.rap/runtime.rwt/org.eclipse.rap.rwt/src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator.java,v >retrieving revision 1.3 >diff -u -r1.3 JSLibraryConcatenator.java >--- src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator.java 12 Apr 2011 14:36:48 -0000 1.3 >+++ src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator.java 17 May 2011 16:33:23 -0000 >@@ -64,7 +64,7 @@ > jsConcatenator = new ByteArrayOutputStream(); > } > >- public void appendJSLibrary( File toWrite, int[] content ) { >+ public void appendJSLibrary( File toWrite, byte[] content ) { > if( isAllowed( toWrite ) ) { > for( int i = 0; i < content.length; i++ ) { > jsConcatenator.write( content[ i ] ); >@@ -97,8 +97,8 @@ > jsConcatenator.write( '\n' ); > } > >- private boolean isLastCharacter( int[] content, int i ) { >- return i == content.length - 1; >+ private static boolean isLastCharacter( byte[] content, int position ) { >+ return position == content.length - 1; > } > > private void initialize() throws UnsupportedEncodingException, IOException { >Index: src/org/eclipse/rwt/internal/resources/ResourceManagerImpl.java >=================================================================== >RCS file: /cvsroot/rt/org.eclipse.rap/runtime.rwt/org.eclipse.rap.rwt/src/org/eclipse/rwt/internal/resources/ResourceManagerImpl.java,v >retrieving revision 1.27 >diff -u -r1.27 ResourceManagerImpl.java >--- src/org/eclipse/rwt/internal/resources/ResourceManagerImpl.java 9 May 2011 09:07:19 -0000 1.27 >+++ src/org/eclipse/rwt/internal/resources/ResourceManagerImpl.java 17 May 2011 16:33:23 -0000 >@@ -79,14 +79,14 @@ > > /** the 'raw' content of the resource. In case of a text resource (charset > * was given) the content is UTF-8 encoded. */ >- private final int[] content; >+ private final byte[] content; > /** the charset in which the resource was encoded before read or null for > * binary resources. */ > private final String charset; > /** the resource's version or null for 'no version' */ > private final Integer version; > >- public Resource( int[] content, String charset, Integer version ) { >+ public Resource( byte[] content, String charset, Integer version ) { > this.charset = charset; > this.content = content; > this.version = version; >@@ -96,7 +96,7 @@ > return charset; > } > >- public int[] getContent() { >+ public byte[] getContent() { > return content; > } > >@@ -131,9 +131,9 @@ > * @return the content of the resource or <code>null</code> if no resource > * with the given <code>name</code> and <code>version</code> exists. > */ >- public int[] findResource( String name, Integer version ) { >+ public byte[] findResource( String name, Integer version ) { > ParamCheck.notNull( name, "name" ); >- int[] result = null; >+ byte[] result = null; > Resource resource = ( Resource )cache.get( createKey( name ) ); > if( resource != null ) { > if( ( version == null && resource.getVersion() == null ) >@@ -197,7 +197,7 @@ > ParamCheck.notNull( is, "is" ); > String key = createKey( name ); > try { >- int[] content = ResourceUtil.readBinary( is ); >+ byte[] content = ResourceUtil.readBinary( is ); > doRegister( name, null, RegisterOptions.NONE, key, content ); > } catch ( IOException e ) { > String text = "Failed to register resource ''{0}''."; >@@ -215,7 +215,7 @@ > boolean compress = shouldCompress( options ); > String key = createKey( name ); > try { >- int[] content = ResourceUtil.read( is, charset, compress ); >+ byte[] content = ResourceUtil.read( is, charset, compress ); > doRegister( name, charset, options, key, content ); > } catch ( IOException ioe ) { > String msg = "Failed to register resource: " + name; >@@ -361,7 +361,7 @@ > if( !repository.containsKey( key ) ) { > boolean compress = shouldCompress( options ); > try { >- int[] content = ResourceUtil.read( name, charset, compress, this ); >+ byte[] content = ResourceUtil.read( name, charset, compress, this ); > doRegister( name, charset, options, key, content ); > } catch ( IOException e ) { > String text = "Failed to register resource ''{0}''."; >@@ -376,7 +376,7 @@ > String charset, > RegisterOptions options, > String key, >- int[] content ) >+ byte[] content ) > throws IOException > { > Integer version = computeVersion( content, options ); >@@ -407,7 +407,7 @@ > } > } > >- private static Integer computeVersion( int[] content, RegisterOptions options ) { >+ private static Integer computeVersion( byte[] content, RegisterOptions options ) { > Integer result = null; > if( content != null && shouldVersion( options ) ) { > int version = 0; >Index: src/org/eclipse/rwt/internal/resources/ResourceUtil.java >=================================================================== >RCS file: /cvsroot/rt/org.eclipse.rap/runtime.rwt/org.eclipse.rap.rwt/src/org/eclipse/rwt/internal/resources/ResourceUtil.java,v >retrieving revision 1.14.2.1 >diff -u -r1.14.2.1 ResourceUtil.java >--- src/org/eclipse/rwt/internal/resources/ResourceUtil.java 17 May 2011 15:37:48 -0000 1.14.2.1 >+++ src/org/eclipse/rwt/internal/resources/ResourceUtil.java 17 May 2011 16:33:23 -0000 >@@ -23,13 +23,13 @@ > public final class ResourceUtil { > > // TODO [rh] avoid passing around the same set of arguments again and again >- static int[] read( String name, >- String charset, >- boolean compress, >- IResourceManager resourceManager ) >+ static byte[] read( String name, >+ String charset, >+ boolean compress, >+ IResourceManager resourceManager ) > throws IOException > { >- int[] result; >+ byte[] result; > if( charset != null ) { > result = readText( name, charset, compress, resourceManager ); > } else { >@@ -38,8 +38,8 @@ > return result; > } > >- static int[] read( InputStream is, String charset, boolean compress ) throws IOException { >- int[] result; >+ static byte[] read( InputStream is, String charset, boolean compress ) throws IOException { >+ byte[] result; > if( charset != null ) { > result = readText( is, charset, compress ); > } else { >@@ -48,14 +48,12 @@ > return result; > } > >- static void write( File toWrite, int[] content ) throws IOException { >+ static void write( File toWrite, byte[] content ) throws IOException { > FileOutputStream fos = new FileOutputStream( toWrite ); > try { > OutputStream out = new BufferedOutputStream( fos ); > try { >- for( int i = 0; i < content.length; i++ ) { >- out.write( content[ i ] ); >- } >+ out.write( content ); > } finally { > out.close(); > } >@@ -70,15 +68,15 @@ > // TODO [rst] Add to concatenation buffer > } > >- private static int[] readText( String name, >- String charset, >- boolean compress, >- IResourceManager resourceManager ) >+ private static byte[] readText( String name, >+ String charset, >+ boolean compress, >+ IResourceManager resourceManager ) > throws IOException > { > // read resource > InputStream is = openStream( name, resourceManager ); >- int[] result; >+ byte[] result; > try { > result = readText( is, charset, compress ); > } finally { >@@ -87,7 +85,7 @@ > return result; > } > >- static int[] readText( InputStream is, String charset, boolean compress ) throws IOException { >+ static byte[] readText( InputStream is, String charset, boolean compress ) throws IOException { > StringBuffer text = new StringBuffer(); > InputStreamReader reader = new InputStreamReader( is, charset ); > BufferedReader br = new BufferedReader( reader ); >@@ -101,37 +99,16 @@ > } finally { > br.close(); > } >- // compress (JavaScript-) buffer if requested > if( compress ) { > compress( text ); > } >- // write just read resource to byte array stream >- byte[] bytes; >- ByteArrayOutputStream baos = new ByteArrayOutputStream(); >- try { >- OutputStreamWriter osw = new OutputStreamWriter( baos, HTTP.CHARSET_UTF_8 ); >- try { >- osw.write( text.toString() ); >- osw.flush(); >- } finally { >- osw.close(); >- } >- bytes = baos.toByteArray(); >- } finally { >- baos.close(); >- } >- // convert byte[] to int[] and return >- int[] result = new int[ bytes.length ]; >- for( int i = 0; i < result.length; i++ ) { >- result[ i ] = ( bytes[ i ] & 0x0ff ); >- } >- return result; >+ return text.toString().getBytes( HTTP.CHARSET_UTF_8 ); > } > >- private static int[] readBinary( String name, IResourceManager resourceManager ) >+ private static byte[] readBinary( String name, IResourceManager resourceManager ) > throws IOException > { >- int[] result; >+ byte[] result; > InputStream is = openStream( name, resourceManager ); > try { > result = readBinary( is ); >@@ -141,7 +118,7 @@ > return result; > } > >- static int[] readBinary( InputStream stream ) throws IOException { >+ static byte[] readBinary( InputStream stream ) throws IOException { > ByteArrayOutputStream bufferedResult = new ByteArrayOutputStream(); > BufferedInputStream bufferedStream = new BufferedInputStream( stream ); > try { >@@ -154,12 +131,7 @@ > } finally { > bufferedStream.close(); > } >- byte[] bytes = bufferedResult.toByteArray(); >- int[] result = new int[ bytes.length ]; >- for( int i = 0; i < bytes.length; i++ ) { >- result[ i ] = bytes[ i ]; >- } >- return result; >+ return bufferedResult.toByteArray(); > } > > private static InputStream openStream( String name, IResourceManager resourceManager ) >#P org.eclipse.rap.rwt.test >Index: src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator_Test.java >=================================================================== >RCS file: /cvsroot/rt/org.eclipse.rap/runtime.rwt.test/org.eclipse.rap.rwt.test/src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator_Test.java,v >retrieving revision 1.1 >diff -u -r1.1 JSLibraryConcatenator_Test.java >--- src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator_Test.java 31 Mar 2011 21:12:55 -0000 1.1 >+++ src/org/eclipse/rwt/internal/resources/JSLibraryConcatenator_Test.java 17 May 2011 16:33:24 -0000 >@@ -22,7 +22,7 @@ > JSLibraryConcatenator concatenator = new JSLibraryConcatenator(); > > concatenator.startJSConcatenation(); >- concatenator.appendJSLibrary( new File( "library.js"), new int[] { character } ); >+ concatenator.appendJSLibrary( new File( "library.js"), new byte[] { ( byte )character } ); > > assertEquals( concatenator.getUncompressed()[ 0 ], character ); > assertEquals( concatenator.getUncompressed()[ 1 ], '\n' ); >@@ -34,7 +34,7 @@ > public void testIgnoreConcatenation() { > JSLibraryConcatenator concatenator = new JSLibraryConcatenator(); > >- concatenator.appendJSLibrary( new File( "library.js"), new int[] { 'a' } ); >+ concatenator.appendJSLibrary( new File( "library.js"), new byte[] { 'a' } ); > > assertEquals( 0, concatenator.getUncompressed().length ); > } >@@ -43,7 +43,7 @@ > JSLibraryConcatenator concatenator = new JSLibraryConcatenator(); > > concatenator.startJSConcatenation(); >- concatenator.appendJSLibrary( new File( "library.js"), new int[ 0 ] ); >+ concatenator.appendJSLibrary( new File( "library.js"), new byte[ 0 ] ); > > assertEquals( 0, concatenator.getUncompressed().length ); > } >@@ -52,7 +52,7 @@ > JSLibraryConcatenator concatenator = new JSLibraryConcatenator(); > > concatenator.startJSConcatenation(); >- concatenator.appendJSLibrary( new File( "content.html"), new int[] { 'a' } ); >+ concatenator.appendJSLibrary( new File( "content.html"), new byte[] { 'a' } ); > > assertEquals( 0, concatenator.getUncompressed().length ); > } >@@ -62,7 +62,7 @@ > > concatenator.startJSConcatenation(); > concatenator.getHashCode(); >- concatenator.appendJSLibrary( new File( "content.js"), new int[] { 'a' } ); >+ concatenator.appendJSLibrary( new File( "content.js"), new byte[] { 'a' } ); > > assertEquals( 0, concatenator.getUncompressed().length ); > } >@@ -72,7 +72,7 @@ > > concatenator.startJSConcatenation(); > concatenator.getCompressed(); >- concatenator.appendJSLibrary( new File( "content.js"), new int[] { 'a' } ); >+ concatenator.appendJSLibrary( new File( "content.js"), new byte[] { 'a' } ); > > assertEquals( 0, concatenator.getUncompressed().length ); > } >@@ -82,7 +82,7 @@ > > concatenator.startJSConcatenation(); > concatenator.getUncompressed(); >- concatenator.appendJSLibrary( new File( "content.js"), new int[] { 'a' } ); >+ concatenator.appendJSLibrary( new File( "content.js"), new byte[] { 'a' } ); > > assertEquals( 0, concatenator.getUncompressed().length ); > } >Index: src/org/eclipse/rwt/internal/resources/ResourceManagerImpl_Test.java >=================================================================== >RCS file: /cvsroot/rt/org.eclipse.rap/runtime.rwt.test/org.eclipse.rap.rwt.test/src/org/eclipse/rwt/internal/resources/ResourceManagerImpl_Test.java,v >retrieving revision 1.16 >diff -u -r1.16 ResourceManagerImpl_Test.java >--- src/org/eclipse/rwt/internal/resources/ResourceManagerImpl_Test.java 13 May 2011 10:47:02 -0000 1.16 >+++ src/org/eclipse/rwt/internal/resources/ResourceManagerImpl_Test.java 17 May 2011 16:33:24 -0000 >@@ -273,8 +273,8 @@ > manager.register( resource, HTTP.CHARSET_UTF_8, RegisterOptions.COMPRESS ); > > File resourceFile = getResourceCopyFile( resource ); >- int[] origin = read( openStream( resource ) ); >- int[] copy = read( resourceFile ); >+ byte[] origin = read( openStream( resource ) ); >+ byte[] copy = read( resourceFile ); > assertTrue( "Resource not registered", manager.isRegistered( resource ) ); > assertTrue( "Resource was not written to disk", resourceFile.exists() ); > assertTrue( "Compressed resource too big", origin.length > copy.length ); >@@ -394,9 +394,9 @@ > > manager.register( ISO_RESOURCE, charset ); > >- int[] expected = read( openStream( UTF_8_RESOURCE ) ); >+ byte[] expected = read( openStream( UTF_8_RESOURCE ) ); > File copiedFile = getResourceCopyFile( ISO_RESOURCE ); >- int[] actual = read( copiedFile ); >+ byte[] actual = read( copiedFile ); > assertEquals( charset, manager.getCharset( ISO_RESOURCE ) ); > assertEquals( expected.length, actual.length ); > assertTrue( Arrays.equals( actual, expected ) ); >@@ -406,8 +406,8 @@ > ResourceManagerImpl manager = getResourceManager( DELIVER_BY_SERVLET ); > manager.register( ISO_RESOURCE, "ISO-8859-1" ); > >- int[] expected = read( openStream( UTF_8_RESOURCE ) ); >- int[] actual = manager.findResource( ISO_RESOURCE, null ); >+ byte[] expected = read( openStream( UTF_8_RESOURCE ) ); >+ byte[] actual = manager.findResource( ISO_RESOURCE, null ); > assertEquals( expected.length, actual.length ); > assertTrue( Arrays.equals( actual, expected ) ); > } >@@ -492,24 +492,24 @@ > /////////////////// > // helping methods > >- private void assertEquals( int[] origin, int[] copy ) { >+ private void assertEquals( byte[] origin, byte[] copy ) { > assertEquals( "Content sizes are different", origin.length, copy.length ); > for( int i = 0; i < copy.length; i++ ) { > assertEquals( "Content is different", origin[ i ], copy[ i ] ); > } > } > >- private static int[] read( File file ) throws IOException { >+ private static byte[] read( File file ) throws IOException { > return read( new FileInputStream( file ) ); > } > >- private static int[] read( InputStream input ) throws IOException { >+ private static byte[] read( InputStream input ) throws IOException { > BufferedInputStream bis = new BufferedInputStream( input ); >- int[] result = null; >+ byte[] result = null; > try { >- result = new int[ bis.available() ]; >+ result = new byte[ bis.available() ]; > for( int i = 0; i < result.length; i++ ) { >- result[ i ] = bis.read(); >+ result[ i ] = ( byte )bis.read(); > } > } finally { > bis.close(); >Index: src/org/eclipse/rwt/internal/resources/ResourceUtil_Test.java >=================================================================== >RCS file: /cvsroot/rt/org.eclipse.rap/runtime.rwt.test/org.eclipse.rap.rwt.test/src/org/eclipse/rwt/internal/resources/ResourceUtil_Test.java,v >retrieving revision 1.9.2.1 >diff -u -r1.9.2.1 ResourceUtil_Test.java >--- src/org/eclipse/rwt/internal/resources/ResourceUtil_Test.java 17 May 2011 15:37:48 -0000 1.9.2.1 >+++ src/org/eclipse/rwt/internal/resources/ResourceUtil_Test.java 17 May 2011 16:33:24 -0000 >@@ -75,8 +75,8 @@ > JSLibraryConcatenator jsConcatenator = RWTFactory.getJSLibraryConcatenator(); > jsConcatenator.startJSConcatenation(); > File file = File.createTempFile( "test", ".js" ); >- ResourceUtil.write( file, getStringAsIntArray( "foo" ) ); >- ResourceUtil.write( file, getStringAsIntArray( "bar" ) ); >+ ResourceUtil.write( file, "foo".getBytes( "UTF-8" ) ); >+ ResourceUtil.write( file, "bar".getBytes( "UTF-8" ) ); > String result = jsConcatenator.getContent(); > assertEquals( "foo\nbar\n", result ); > } >@@ -84,36 +84,33 @@ > public void testReadText() throws IOException { > String input = createTestString( 10000 ); > InputStream inputStream = new ByteArrayInputStream( input.getBytes( "UTF-8" ) ); >- int[] result = ResourceUtil.readText( inputStream, "UTF-8", false ); >- byte[] bytes = toByteArray( result ); >- assertEquals( input, new String( bytes ) ); >+ >+ byte[] result = ResourceUtil.readText( inputStream, "UTF-8", false ); >+ >+ assertEquals( input, new String( result ) ); > } > >- private static byte[] toByteArray( int[] result ) { >- byte[] bytes = new byte[ result.length ]; >- for( int i = 0; i < bytes.length; i++ ) { >- bytes[ i ] = ( byte )result[ i ]; >- } >- return bytes; >+ public void testWriteText() throws IOException { >+ String input = createTestString( 10000 ); >+ byte[] content = input.getBytes( "UTF-8" ); >+ File tempFile = File.createTempFile( "rap-", ".test" ); >+ tempFile.deleteOnExit(); >+ >+ ResourceUtil.write( tempFile, content ); >+ >+ byte[] result = ResourceUtil.readText( new FileInputStream( tempFile ), "UTF-8", false ); >+ assertEquals( input, new String( result ) ); > } > > private static String createTestString( int length ) { > StringBuffer buffer = new StringBuffer( length ); >- for( int i = 0; i < length; i++ ) { >- buffer.append( (char) ( 32 + ( i % 32 ) ) ); >+ buffer.append( 'Ã' ); >+ for( int i = 1; i < length; i++ ) { >+ buffer.append( ( char )( 32 + ( i % 32 ) ) ); > } > return buffer.toString(); > } > >- private static int[] getStringAsIntArray( String string ) { >- byte[] bytes = string.getBytes(); >- int[] content = new int[ bytes.length ]; >- for( int i = 0; i < content.length; i++ ) { >- content[ i ] = ( bytes[ i ] & 0x0ff ); >- } >- return content; >- } >- > protected void setUp() throws Exception { > Fixture.createApplicationContext(); > Fixture.createServiceContext();
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 346104
:
195881
| 195890