static List<Integer> reservoir_sampling(InputStream sin, int k)
			throws IOException, ClassNotFoundException {
		
		List<Integer> R = new ArrayList<Integer>();
		
		ObjectInputStream osin = new ObjectInputStream(sin);
		// Store the first k elements.
		Integer x = (Integer) readObjectSilently(osin);
		for (int i = 0; i < k && x != null; ++i) {
			R.add(x);
			x = (Integer) readObjectSilently(osin);
		}

		// After the first k elements.
		int element_num = k + 1;
		x = (Integer) readObjectSilently(osin);
		while (x != null) {
			Random gen = new Random(); // random num generator.
			// Generate random int in [0, element_num].
			int tar = gen.nextInt(++element_num);
			if (tar < k) {
				R.set(tar, x);
			}
			
			x = (Integer) readObjectSilently(osin);
		}
		
		// Close "osin" silently
		close(osin);
		return R;
	}