15c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes/* Licensed to the Apache Software Foundation (ASF) under one or more
25c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * contributor license agreements.  See the NOTICE file distributed with
35c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * this work for additional information regarding copyright ownership.
45c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0
55c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * (the "License"); you may not use this file except in compliance with
65c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * the License.  You may obtain a copy of the License at
75c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes *
85c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
95c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes *
105c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * Unless required by applicable law or agreed to in writing, software
115c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
125c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * See the License for the specific language governing permissions and
145c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes * limitations under the License.
155c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes */
16ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamathpackage org.apache.harmony.tests.java.util;
175c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
185c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughesimport java.io.IOException;
195c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughesimport java.io.Reader;
205c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughesimport java.util.Scanner;
215c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
225c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughesimport junit.framework.TestCase;
235c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
245c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughespublic class ScannerParseLargeFileBenchmarkTest extends TestCase {
255c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
265c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes    /**
274b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller     * Check whether the Scanner will exhaust all heap memory when parsing a
284b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller     * large file.
295c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes     */
305c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes    public void testParseLargeFile() throws Exception {
314b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller        FakeLargeFile reader = new FakeLargeFile();
325c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        String delimiter = "\r?\n";
335c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        Scanner scanner = new Scanner(reader).useDelimiter(delimiter);
345c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
355c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        while (scanner.hasNext()) {
365c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes            scanner.next();
375c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        }
385c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        scanner.close();
395c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        reader.close();
405c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes    }
415c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
424b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller    private static class FakeLargeFile extends Reader {
434b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller        private static final char[] CONTENT = "large file!\n".toCharArray();
444b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller        private static final int FILE_LENGTH = 192 * 1024 * 1024; // 192 MB
455c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
465c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        private int count = 0;
475c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
485c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        @Override
495c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        public void close() throws IOException {
505c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        }
515c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
525c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        @Override
534b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller        public int read(char[] buffer, int offset, int length) {
544b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            if (count >= FILE_LENGTH) {
555c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes                return -1;
565c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes            }
575c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes
584b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            final int charsToRead = Math.min(FILE_LENGTH - count, length);
594b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            int bufferIndex = offset;
604b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            int contentIndex = count % CONTENT.length;
614b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            int charsRead = 0;
624b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            while (charsRead < charsToRead) {
634b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller                buffer[bufferIndex++] = CONTENT[contentIndex++];
644b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller                if (contentIndex == CONTENT.length) {
654b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller                    contentIndex = 0;
664b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller                }
674b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller                charsRead++;
684b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            }
694b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            count += charsRead;
704b116a2f5d3c6e2a0a7fe39d5eb956563138d542Neil Fuller            return charsToRead;
715c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes        }
725c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes    }
735c8452e1fca6a47ecbe71ac7f71e378b3be16ec7Elliott Hughes}
74