Changeset 5278


Ignore:
Timestamp:
Aug 15, 2016, 1:29:00 AM (6 years ago)
Author:
Christian Schulte
Message:

o Updated to use 'InputStream?' and 'OutputStream?' instead of 'FileChannel?'.

See #104

Location:
jomc-tools/trunk/jomc-tools/src/main/java/org/jomc/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • jomc-tools/trunk/jomc-tools/src/main/java/org/jomc/tools/ClassFileProcessor.java

    r5274 r5278  
    3535import java.io.File;
    3636import java.io.FileInputStream;
     37import java.io.FileOutputStream;
    3738import java.io.IOException;
    3839import java.io.InputStream;
    39 import java.io.RandomAccessFile;
    4040import java.net.URL;
    41 import java.nio.ByteBuffer;
    42 import java.nio.channels.FileChannel;
    4341import java.nio.channels.FileLock;
    4442import java.text.MessageFormat;
     
    25092507    {
    25102508        try ( final FileInputStream in = new FileInputStream( classFile );
    2511               final FileChannel fileChannel = in.getChannel();
    2512               final FileLock fileLock = fileChannel.lock( 0, classFile.length(), true ) )
     2509              final FileLock fileLock = in.getChannel().lock( 0, classFile.length(), true ) )
    25132510        {
    25142511            return new ClassParser( in, classFile.getAbsolutePath() ).parse();
     
    25182515    private void writeJavaClass( final JavaClass javaClass, final File classFile ) throws IOException
    25192516    {
    2520         final ByteArrayOutputStream byteStream = new ByteArrayOutputStream( 524288 );
    2521         javaClass.dump( byteStream );
    2522         byteStream.close();
    2523 
    2524         final byte[] bytes = byteStream.toByteArray();
    2525 
    2526         try ( final RandomAccessFile randomAccessFile = new RandomAccessFile( classFile, "rw" );
    2527               final FileChannel fileChannel = randomAccessFile.getChannel();
    2528               final FileLock fileLock = fileChannel.lock() )
    2529         {
    2530             fileChannel.truncate( bytes.length );
    2531             fileChannel.position( 0L );
    2532             fileChannel.write( ByteBuffer.wrap( bytes ) );
    2533             fileChannel.force( true );
     2517        try ( final FileOutputStream out = new FileOutputStream( classFile );
     2518              final FileLock fileLock = out.getChannel().lock() )
     2519        {
     2520            javaClass.dump( out );
    25342521        }
    25352522    }
  • jomc-tools/trunk/jomc-tools/src/main/java/org/jomc/tools/ResourceFileProcessor.java

    r5274 r5278  
    3131package org.jomc.tools;
    3232
    33 import java.io.ByteArrayOutputStream;
    3433import java.io.File;
     34import java.io.FileOutputStream;
    3535import java.io.IOException;
    36 import java.io.RandomAccessFile;
    37 import java.nio.ByteBuffer;
    38 import java.nio.channels.FileChannel;
    3936import java.nio.channels.FileLock;
    4037import java.text.MessageFormat;
     
    539536        throws IOException
    540537    {
    541         final ByteArrayOutputStream byteStream = new ByteArrayOutputStream( 524288 );
    542         properties.store( byteStream, comments );
    543         byteStream.close();
    544 
    545         final byte[] bytes = byteStream.toByteArray();
    546 
    547         try ( final RandomAccessFile randomAccessFile = new RandomAccessFile( propertiesFile, "rw" );
    548               final FileChannel fileChannel = randomAccessFile.getChannel();
    549               final FileLock fileLock = fileChannel.lock() )
    550         {
    551             fileChannel.truncate( bytes.length );
    552             fileChannel.position( 0L );
    553             fileChannel.write( ByteBuffer.wrap( bytes ) );
    554             fileChannel.force( true );
     538        try ( final FileOutputStream out = new FileOutputStream( propertiesFile );
     539              final FileLock fileLock = out.getChannel().lock() )
     540        {
     541            properties.store( out, comments );
     542            out.getChannel().force( true );
    555543        }
    556544    }
  • jomc-tools/trunk/jomc-tools/src/main/java/org/jomc/tools/SourceFileProcessor.java

    r5274 r5278  
    3232
    3333import java.io.File;
     34import java.io.FileInputStream;
     35import java.io.FileOutputStream;
    3436import java.io.IOException;
    35 import java.io.RandomAccessFile;
     37import java.io.InputStreamReader;
     38import java.io.Reader;
    3639import java.io.StringWriter;
    37 import java.nio.ByteBuffer;
    38 import java.nio.channels.FileChannel;
    3940import java.nio.channels.FileLock;
    40 import java.nio.charset.Charset;
    4141import java.text.MessageFormat;
    4242import java.util.ResourceBundle;
     
    934934            }
    935935
    936             final Charset charset = Charset.forName( getInputEncoding() );
    937936            final StringBuilder appendable =
    938937                new StringBuilder( file.length() > 0L ? Long.valueOf( file.length() ).intValue() : 1 );
    939938
    940             final ByteBuffer buf = ByteBuffer.allocateDirect( 524288 );
    941 
    942             try ( final RandomAccessFile randomAccessFile = new RandomAccessFile( file, "r" );
    943                   final FileChannel fileChannel = randomAccessFile.getChannel();
    944                   final FileLock fileLock = fileChannel.lock( 0L, file.length(), true ) )
    945             {
    946                 fileChannel.position( 0L );
    947 
    948                 for ( int read = fileChannel.read( buf ); read >= 0; buf.position( 0 ), read = fileChannel.read( buf ) )
    949                 {
    950                     final byte[] chunk = new byte[ read ];
    951                     buf.position( 0 );
    952                     buf.get( chunk );
    953                     appendable.append( new String( chunk, charset ) );
     939            final char[] chars = new char[ 524288 ];
     940
     941            try ( final FileInputStream in = new FileInputStream( file );
     942                  final Reader reader = new InputStreamReader( in, getInputEncoding() );
     943                  final FileLock fileLock = in.getChannel().lock( 0L, file.length(), true ) )
     944            {
     945                for ( int read = reader.read( chars ); read >= 0; read = reader.read( chars ) )
     946                {
     947                    appendable.append( chars, 0, read );
    954948                }
    955949
     
    969963            }
    970964
    971             final byte[] bytes = content.getBytes( getOutputEncoding() );
    972 
    973             try ( final RandomAccessFile randomAccessFile = new RandomAccessFile( file, "rw" );
    974                   final FileChannel fileChannel = randomAccessFile.getChannel();
    975                   final FileLock fileLock = fileChannel.lock() )
    976             {
    977                 fileChannel.truncate( bytes.length );
    978                 fileChannel.position( 0L );
    979                 fileChannel.write( ByteBuffer.wrap( bytes ) );
    980                 fileChannel.force( true );
     965            try ( final FileOutputStream out = new FileOutputStream( file );
     966                  final FileLock fileLock = out.getChannel().lock() )
     967            {
     968                out.write( content.getBytes( getOutputEncoding() ) );
     969                out.getChannel().force( true );
    981970            }
    982971        }
Note: See TracChangeset for help on using the changeset viewer.