Skip to content

Commit

Permalink
Store the system encoding in encoding.info
Browse files Browse the repository at this point in the history
  • Loading branch information
merks committed Nov 5, 2023
1 parent 751267f commit c8e2e87
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 141 deletions.
Expand Up @@ -126,7 +126,7 @@ public AgentImpl(AgentManagerImpl agentManager, File location)
this.agentManager = agentManager;
this.location = location;

bundlePoolMap = new PersistentMap<>(new File(location, "pools.info")) //$NON-NLS-1$
bundlePoolMap = new PersistentMap<>(new File(location, "pools.info"), agentManager.getCharset()) //$NON-NLS-1$
{
@Override
protected BundlePool createElement(String key, String extraInfo)
Expand Down Expand Up @@ -156,7 +156,7 @@ protected void initializeFirstTime()
}
};

profileMap = new PersistentMap<>(new File(location, "profiles.info")) //$NON-NLS-1$
profileMap = new PersistentMap<>(new File(location, "profiles.info"), agentManager.getCharset()) //$NON-NLS-1$
{
@Override
protected Profile createElement(String profileID, String extraInfo)
Expand Down
Expand Up @@ -32,6 +32,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -49,6 +51,8 @@ public class AgentManagerImpl implements AgentManager

public static AgentManager instance;

private final Charset charset;

private final PersistentMap<Agent> agentMap;

private final File defaultAgentLocation;
Expand All @@ -68,11 +72,41 @@ public AgentManagerImpl(final File userHome)
{
defaultAgentLocation = new File(userHome, ".p2"); //$NON-NLS-1$

File encodingFile = new File(defaultAgentLocation, "encoding.info"); //$NON-NLS-1$
Charset charset;
try
{
charset = new PersistentMap<Charset>(encodingFile, StandardCharsets.UTF_8)
{
{
load();
}

@Override
protected Charset createElement(String key, String extraInfo)
{
return Charset.forName(key);
}

@Override
protected void initializeFirstTime()
{
addElement(Charset.defaultCharset().name(), null);
}
}.getElements().iterator().next();
}
catch (Exception ex)
{
charset = Charset.defaultCharset();
}

this.charset = charset;

File folder = P2CorePlugin.getUserStateFolder(userHome);
File infoFile = new File(folder, "agents.info"); //$NON-NLS-1$
defaultsFile = new File(folder, "defaults.info"); //$NON-NLS-1$

agentMap = new PersistentMap<Agent>(infoFile)
agentMap = new PersistentMap<Agent>(infoFile, charset)
{
@Override
protected Agent loadElement(String key, String extraInfo)
Expand Down Expand Up @@ -142,6 +176,11 @@ private void initializeFirstTime(File location)
agentMap.load();
}

public Charset getCharset()
{
return charset;
}

public PersistentMap<Agent> getAgentMap()
{
return agentMap;
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -39,6 +40,8 @@
*/
public abstract class PersistentMap<E>
{
private final Charset charset;

private final File file;

private final File tempFile;
Expand All @@ -47,8 +50,9 @@ public abstract class PersistentMap<E>

private final Map<String, E> elements = new LinkedHashMap<>();

public PersistentMap(File file)
public PersistentMap(File file, Charset charset)
{
this.charset = charset;
this.file = file;

if (file != null)
Expand Down Expand Up @@ -125,6 +129,11 @@ public final File getFile()
return file;
}

public final Charset getCharset()
{
return charset;
}

public final synchronized Set<String> getElementKeys()
{
return new HashSet<>(elements.keySet());
Expand Down Expand Up @@ -219,7 +228,7 @@ private void load(KeyHandler handler)

try
{
infoReader = new FileReader(file);
infoReader = new FileReader(file, charset);
BufferedReader bufferedReader = new BufferedReader(infoReader);

String line;
Expand Down Expand Up @@ -279,7 +288,7 @@ public void save(String addedKey, final String removedKey)
}
}

tempWriter = new FileWriter(tempFile);
tempWriter = new FileWriter(tempFile, charset);
BufferedWriter bufferedWriter = new BufferedWriter(tempWriter);

List<String> sortedKeys = new ArrayList<>(elements.keySet());
Expand Down Expand Up @@ -379,7 +388,7 @@ private FileWriter lock()
{
try
{
return new FileWriter(lockFile);
return new FileWriter(lockFile, charset);
}
catch (IOException ex)
{
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit c8e2e87

Please sign in to comment.