DevURandom.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.5.0).
03  * Copyright (c) 2007-2016 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.internal.util;
21 
22 import java.io.BufferedInputStream;
23 import java.io.Closeable;
24 import java.io.DataInputStream;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.UncheckedIOException;
28 
29 import org.jenetics.util.Random64;
30 
31 /**
32  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
33  * @since 3.0
34  * @version 3.0
35  */
36 public class DevURandom extends Random64 implements Closeable {
37     private static final long serialVersionUID = 1L;
38 
39     private final DataInputStream _input = input();
40 
41     private static DataInputStream input() {
42         try {
43             return new DataInputStream(
44                 new BufferedInputStream(
45                     new FileInputStream("/dev/urandom")
46                 )
47             );
48         } catch (IOException e) {
49             throw new UncheckedIOException(e);
50         }
51     }
52 
53     @Override
54     public long nextLong() {
55         try {
56             return _input.readLong();
57         } catch (IOException e) {
58             throw new UncheckedIOException(e);
59         }
60     }
61 
62     @Override
63     public void close() throws IOException {
64         _input.close();
65     }
66 
67 }