@Test
  public void regularReader() throws Exception {
    CloseableRegistry r = new CloseableRegistry();
    try {
      ByteArrayInputStream is = new ByteArrayInputStream(HELLO_EN_NAT);
      r.register(new InputStreamWrapper(is));
      UnicodeInputStreamReader reader = new UnicodeInputStreamReader(is);
      r.register(new ReaderWrapper(reader));

      assertNull(reader.getDecodingStrategy());
      assertNull(reader.getRequestedSignatureCharset());
      assertNull(reader.getSignatureCharset());

      assertTrue(reader.ready());

      assertFalse(reader.markSupported());

      try {
        reader.mark(0);
        fail();
      } catch (IOException ex) {
        // Desired.
      }

      try {
        reader.reset();
        fail();
      } catch (IOException ex) {
        // Desired.
      }

      assertEquals(HELLO_EN.charAt(0), reader.read());

      assertEquals(1, reader.skip(1));

      char[] charArray = new char[1];
      assertEquals(1, reader.read(charArray));
      assertEquals(HELLO_EN.charAt(2), charArray[0]);

      charArray = new char[3];
      assertEquals(1, reader.read(charArray, 1, 1));
      assertEquals(HELLO_EN.charAt(3), charArray[1]);

      CharBuffer charBuffer = CharBuffer.allocate(10);
      assertEquals(1, reader.read(charBuffer));
      assertEquals(HELLO_EN.charAt(4), charBuffer.get(0));

      assertEquals(-1, reader.read());
      assertEquals(-1, reader.read(charArray));
      assertEquals(-1, reader.read(charArray, 1, 1));
      assertEquals(-1, reader.read(charBuffer));
      assertFalse(reader.ready());

      reader.close();
      reader.close();

      // Ensure that close() has closed the stream, by trying to
      // read from the reader: this is not testing whether
      // read() fails; it tests whether close() worked.
      try {
        reader.read();
        fail();
      } catch (IOException ex) {
        // Desired.
      }

      try {
        reader.ready();
        fail();
      } catch (IOException ex) {
        // Desired.
      }
    } finally {
      r.close();
    }
  }
Exemple #2
0
  public void load() {
    BufferedReader buffer = null;
    try {
      if (file.getParentFile() != null) {
        file.getParentFile().mkdirs();
      }

      if (!file.exists() && !file.createNewFile()) {
        return;
      }

      if (file.canRead()) {
        UnicodeInputStreamReader input =
            new UnicodeInputStreamReader(new FileInputStream(file), defaultEncoding);
        defaultEncoding = input.getEncoding();
        buffer = new BufferedReader(input);

        String line;
        Map<String, Property> currentMap = null;

        while (true) {
          line = buffer.readLine();

          if (line == null) {
            break;
          }

          int nameStart = -1, nameEnd = -1;
          boolean skip = false;
          boolean quoted = false;
          for (int i = 0; i < line.length() && !skip; ++i) {
            if (Character.isLetterOrDigit(line.charAt(i))
                || ALLOWED_CHARS.indexOf(line.charAt(i)) != -1
                || (quoted && line.charAt(i) != '"')) {
              if (nameStart == -1) {
                nameStart = i;
              }

              nameEnd = i;
            } else if (Character.isWhitespace(line.charAt(i))) {
              // ignore space charaters
            } else {
              switch (line.charAt(i)) {
                case '#':
                  skip = true;
                  continue;

                case '"':
                  if (quoted) {
                    quoted = false;
                  }
                  if (!quoted && nameStart == -1) {
                    quoted = true;
                  }
                  break;

                case '{':
                  String scopeName = line.substring(nameStart, nameEnd + 1);

                  currentMap = categories.get(scopeName);
                  if (currentMap == null) {
                    currentMap = new TreeMap<String, Property>();
                    categories.put(scopeName, currentMap);
                  }

                  break;

                case '}':
                  currentMap = null;
                  break;

                case '=':
                  String propertyName = line.substring(nameStart, nameEnd + 1);

                  if (currentMap == null) {
                    throw new RuntimeException("property " + propertyName + " has no scope");
                  }

                  Property prop = new Property();
                  prop.setName(propertyName);
                  prop.value = line.substring(i + 1);
                  i = line.length();

                  currentMap.put(propertyName, prop);

                  break;

                default:
                  throw new RuntimeException("unknown character " + line.charAt(i));
              }
            }
          }
          if (quoted) {
            throw new RuntimeException("unmatched quote");
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (buffer != null) {
        try {
          buffer.close();
        } catch (IOException e) {
        }
      }
    }
  }