Skip to content

Commit

Permalink
Cleanup utf charset name and utilities usage
Browse files Browse the repository at this point in the history
Switch to using charset constant defined in java.nio.charset.StandardCharsets.
Remove custom helper class for charsets,
Cleanup handling of UnsupportedEncodingException.
Switch FileSystemAbstraction to use charset instead of encodinng.
Update code as much as possible to use org.neo4j.helpers.UTF8.
  • Loading branch information
MishaDemianenko committed Oct 15, 2015
1 parent 7f8cc66 commit b13d1b2
Show file tree
Hide file tree
Showing 95 changed files with 420 additions and 476 deletions.
Expand Up @@ -20,7 +20,7 @@
package org.neo4j.bolt.v1.packstream;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* PackStream is a messaging serialisation format heavily inspired by MessagePack.
Expand Down Expand Up @@ -155,8 +155,6 @@ public class PackStream
private static final long MINUS_2_TO_THE_15 = -32768L;
private static final long MINUS_2_TO_THE_31 = -2147483648L;

public static final Charset UTF_8 = Charset.forName( "UTF-8" );

private PackStream()
{
}
Expand Down Expand Up @@ -234,7 +232,7 @@ public void pack( String value ) throws IOException
if ( value == null ) { packNull(); }
else
{
byte[] utf8 = value.getBytes( UTF_8 );
byte[] utf8 = value.getBytes( StandardCharsets.UTF_8 );
packTextHeader( utf8.length );
packRaw( utf8 );
}
Expand Down Expand Up @@ -500,7 +498,7 @@ public double unpackDouble() throws IOException

public String unpackText() throws IOException
{
return new String( unpackUTF8(), UTF_8 );
return new String( unpackUTF8(), StandardCharsets.UTF_8 );
}

private int unpackBytesHeader() throws IOException
Expand Down
Expand Up @@ -42,8 +42,6 @@
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertEquals;

import static org.neo4j.bolt.v1.packstream.PackStream.UTF_8;

public class PackStreamTest
{

Expand Down
Expand Up @@ -19,8 +19,15 @@
*/
package org.neo4j.browser;

import org.apache.commons.collections4.CollectionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -33,23 +40,15 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections4.CollectionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Notification;
import org.neo4j.graphdb.QueryExecutionException;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.impl.notification.NotificationCode;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.impl.util.Charsets;
import org.neo4j.test.TestGraphDatabaseFactory;

import static java.lang.String.format;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
Expand Down Expand Up @@ -87,7 +86,7 @@ public FileVisitResult visitFile( Path file, BasicFileAttributes attributes ) th
String fileName = file.getFileName().toString();
if ( fileName.endsWith( ".html" ) )
{
String content = FileUtils.readTextFile( file.toFile(), Charsets.UTF_8 );
String content = FileUtils.readTextFile( file.toFile(), StandardCharsets.UTF_8 );
Elements cypherElements = Jsoup.parse( content ).select( "pre.runnable" )
.not( ".standalone-example" );
for ( Element cypherElement : cypherElements )
Expand Down
7 changes: 4 additions & 3 deletions community/csv/src/main/java/org/neo4j/csv/reader/Magic.java
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -49,9 +50,9 @@ public class Magic
/** A couple of BOM magics */
public static final Magic BOM_UTF_32_BE = define( "BOM_UTF_32_BE", forName( "UTF-32" ), 0x0, 0x0, 0xFE, 0xFF );
public static final Magic BOM_UTF_32_LE = define( "BOM_UTF_32_LE", forName( "UTF-32" ), 0xFF, 0xFE, 0x0, 0x0 );
public static final Magic BOM_UTF_16_BE = define( "BOM_UTF_16_BE", forName( "UTF-16" ), 0xFE, 0xFF );
public static final Magic BOM_UTF_16_LE = define( "BOM_UTF_16_LE", forName( "UTF-16" ), 0xFF, 0xFE );
public static final Magic BOM_UTF_8 = define( "BOM_UTF8", forName( "UTF-8" ), 0xEF, 0xBB, 0xBF );
public static final Magic BOM_UTF_16_BE = define( "BOM_UTF_16_BE", StandardCharsets.UTF_16BE, 0xFE, 0xFF );
public static final Magic BOM_UTF_16_LE = define( "BOM_UTF_16_LE", StandardCharsets.UTF_16LE, 0xFF, 0xFE );
public static final Magic BOM_UTF_8 = define( "BOM_UTF8", StandardCharsets.UTF_8, 0xEF, 0xBB, 0xBF );

/**
* Defines a magic signature which can later be detected in {@link #of(File)} and {@link #of(byte[])}.
Expand Down
Expand Up @@ -32,20 +32,20 @@
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.neo4j.test.TestDirectory;

import static java.util.Arrays.copyOfRange;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import static java.util.Arrays.copyOfRange;

public class ReadablesTest
{
@Test
Expand Down Expand Up @@ -148,13 +148,13 @@ public void shouldTrackPosition() throws Exception
@Test
public void shouldComplyWithUtf8CharsetForExample() throws Exception
{
shouldComplyWithSpecifiedCharset( Charset.forName( "utf-8" ) );
shouldComplyWithSpecifiedCharset( StandardCharsets.UTF_8 );
}

@Test
public void shouldComplyWithIso88591CharsetForExample() throws Exception
{
shouldComplyWithSpecifiedCharset( Charset.forName( "iso-8859-1" ) );
shouldComplyWithSpecifiedCharset( StandardCharsets.ISO_8859_1 );
}

@Test
Expand Down
Expand Up @@ -21,7 +21,7 @@ package org.neo4j.cypher.internal.compiler.v3_0.spi

import java.io._
import java.net.{CookieHandler, CookieManager, CookiePolicy, URL}
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import java.util.zip.{GZIPInputStream, InflaterInputStream}

Expand Down Expand Up @@ -55,10 +55,10 @@ class CSVResources(cleaner: TaskCloser) extends ExternalResource {
val inputStream = openStream(url)

val reader = if (url.getProtocol == "file") {
Readables.files(Charset.forName("UTF-8"), Paths.get(url.toURI).toFile)
Readables.files(StandardCharsets.UTF_8, Paths.get(url.toURI).toFile)
}
else
Readables.wrap(inputStream, url.toString, Charset.forName("UTF-8"))
Readables.wrap(inputStream, url.toString, StandardCharsets.UTF_8)
val delimiter: Char = fieldTerminator.map(_.charAt(0)).getOrElse(CSVResources.DEFAULT_FIELD_TERMINATOR)
val seeker = CharSeekers.charSeeker(reader, CSVResources.defaultConfig, true)
val extractor = new Extractors(delimiter).string()
Expand Down
Expand Up @@ -20,6 +20,7 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -97,7 +98,7 @@ private static String waitForUserInput( final String textToSystemOut )
throws Exception
{
System.out.print( textToSystemOut );
return new BufferedReader( new InputStreamReader( System.in, "UTF-8" ) )
return new BufferedReader( new InputStreamReader( System.in, StandardCharsets.UTF_8 ) )
.readLine();
}

Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

import org.neo4j.graphdb.GraphDatabaseService;
Expand Down Expand Up @@ -155,9 +156,9 @@ public static String createGraphViz( String title,
try
{
return "." + title + "\n[\"dot\", \""
+ (safeTitle + "-" + identifier).replace( " ", "-" )
+ ".svg\", \"neoviz\", \"" + graphvizOptions + "\"]\n"
+ "----\n" + out.toString( "UTF-8" ) + "----\n";
+ (safeTitle + "-" + identifier).replace( " ", "-" )
+ ".svg\", \"neoviz\", \"" + graphvizOptions + "\"]\n"
+ "----\n" + out.toString( StandardCharsets.UTF_8.name() ) + "----\n";
}
catch ( UnsupportedEncodingException e )
{
Expand Down
Expand Up @@ -24,9 +24,9 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -76,7 +76,7 @@ public ConfigurationParser( Iterable<String> format )
try
{
method = type.getMethod( name, String.class );
args = new String[] { parts[1] };
args = new String[]{parts[1]};
}
catch ( NoSuchMethodException nsm )
{
Expand Down Expand Up @@ -126,7 +126,10 @@ public ConfigurationParser( Iterable<String> format )

public final StyleParameter[] styles( StyleParameter... params )
{
if ( params == null ) params = new StyleParameter[0];
if ( params == null )
{
params = new StyleParameter[0];
}
StyleParameter[] result = styles.toArray( new StyleParameter[styles.size() + params.length] );
System.arraycopy( params, 0, result, styles.size(), params.length );
return result;
Expand Down Expand Up @@ -159,11 +162,13 @@ public String getTitle( Relationship container )
public void nodePropertyFilter( String nodeProperties )
{
final String nodePropertiesString = nodeProperties;
styles.add( new StyleParameter.NodePropertyFilter() {
public boolean acceptProperty(String key) {
return Arrays.asList(nodePropertiesString.split(",")).contains(key);
}
});
styles.add( new StyleParameter.NodePropertyFilter()
{
public boolean acceptProperty( String key )
{
return Arrays.asList( nodePropertiesString.split( "," ) ).contains( key );
}
} );
}

public void reverseOrder( String... typeNames )
Expand Down Expand Up @@ -242,18 +247,18 @@ private String getSpecial( String attribute, PropertyContainer container )
{
if ( container instanceof Node )
{
return "" + ( (Node) container ).getId();
return "" + ((Node) container).getId();
}
else if ( container instanceof Relationship )
{
return "" + ( (Relationship) container ).getId();
return "" + ((Relationship) container).getId();
}
}
else if ( attribute.equals( "type" ) )
{
if ( container instanceof Relationship )
{
return ( (Relationship) container ).getType().name();
return ((Relationship) container).getType().name();
}
}
return "@" + attribute;
Expand All @@ -278,16 +283,13 @@ private static BufferedReader fileReader( File file )
{
try
{
return new BufferedReader( new InputStreamReader( new FileInputStream( file ), "UTF-8" ) );
return new BufferedReader(
new InputStreamReader( new FileInputStream( file ), StandardCharsets.UTF_8 ) );
}
catch ( FileNotFoundException e )
{
return null;
}
catch ( UnsupportedEncodingException e )
{
throw new RuntimeException( e );
}
}

@Override
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

import org.neo4j.visualization.Visualizer;
import org.neo4j.walk.Walker;
Expand Down Expand Up @@ -87,13 +88,14 @@ public void emit( OutputStream outputStream, Walker walker )
}
else
{
emit( walker, new GraphvizRenderer( style, new PrintStream( outputStream, true, "UTF-8" ) ) );
emit( walker, new GraphvizRenderer( style,
new PrintStream( outputStream, true, StandardCharsets.UTF_8.name() ) ) );
}
}

private void emit( Walker walker, GraphvizRenderer renderer )
throws IOException
{
walker.accept( new Visualizer<IOException>( renderer ) );
walker.accept( new Visualizer<>( renderer ) );
}
}
Expand Up @@ -19,13 +19,15 @@
*/
package org.neo4j.tooling;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -40,13 +42,14 @@
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.Charsets;
import org.neo4j.test.TargetDirectory;
import org.neo4j.test.TargetDirectory.TestDirectory;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.tooling.ImportTool.Options;
import org.neo4j.unsafe.impl.batchimport.Configuration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.neo4j.helpers.ArrayUtil.join;
import static org.neo4j.helpers.collection.IteratorUtil.asSet;
import static org.neo4j.helpers.collection.IteratorUtil.count;
Expand All @@ -55,10 +58,6 @@
import static org.neo4j.tooling.GlobalGraphOperations.at;
import static org.neo4j.tooling.ImportTool.MULTI_FILE_DELIMITER;

import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;

public class ImportToolDocIT
{
private static final int NODE_COUNT = 6;
Expand Down Expand Up @@ -736,7 +735,7 @@ public void printOptionsForManual() throws Exception
private void printFileWithPathsRemoved( File badFile, String realDir, String destinationFileName )
throws IOException
{
String contents = readTextFile( badFile, Charsets.UTF_8 );
String contents = readTextFile( badFile, StandardCharsets.UTF_8 );
String cleanedContents = contents.replace( realDir + File.separator, "" );
writeToFile( file( "ops", destinationFileName ), cleanedContents, false );
}
Expand Down

0 comments on commit b13d1b2

Please sign in to comment.