Skip to content

Avoid FileReader #1537

Description

@elharo

Noticed you're using FileReader in com.google.cloud.ServiceOptions and possibly elsewhere. This is potentially buggy because it depends on the platform default encoding and thus behavior can change from one system to the next. Instead, try an InputStreamReader with a specified encoding chained to a FileInputStream.

protected static String googleCloudProjectId() {
    File configDir;
    if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
      configDir = new File(System.getenv("CLOUDSDK_CONFIG"));
    } else if (isWindows() && System.getenv().containsKey("APPDATA")) {
      configDir = new File(System.getenv("APPDATA"), "gcloud");
    } else {
      configDir = new File(System.getProperty("user.home"), ".config/gcloud");
    }
    String activeConfig = activeGoogleCloudConfig(configDir);
    FileReader fileReader = null;
    try {
      fileReader = new FileReader(new File(configDir, "configurations/config_" + activeConfig));
    } catch (FileNotFoundException newConfigFileNotFoundEx) {
      try {
        fileReader = new FileReader(new File(configDir, "properties"));
      } catch (FileNotFoundException oldConfigFileNotFoundEx) {
        // ignore
      }
}

Activity

  1. added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on May 24, 2017
  2. garrettjonesgoogle commented on Aug 10, 2017

    @garrettjonesgoogle
    Contributor

    @elharo Do we know that the encoding of the configuration files will always be the same?

  3. elharo commented on Aug 10, 2017

    @elharo
    ContributorAuthor

    The problem is that you can't assume it is the native encoding of the platform. I.e. there is no guarantee that FileReader() does the right thing.

    You'd have to ask gcloud what character set they use.

  4. added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    and removed
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Nov 9, 2017
  5. yihanzhen commented on Jan 11, 2018

    @yihanzhen
    Contributor

    The feature request is added to the backlog. The issue will be closed but is linked in the backlog and can continue to be used for comment and discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: corepriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions