Skip to content

Commit

Permalink
Merge pull request #38 from PorkStudios/dev/natives-cleanup
Browse files Browse the repository at this point in the history
Natives cleanup
  • Loading branch information
DaMatrix authored Feb 15, 2020
2 parents 65d0934 + 830f936 commit ada6412
Show file tree
Hide file tree
Showing 127 changed files with 5,546 additions and 2,229 deletions.
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ test_out/
# project root
/src/

#native libraries
/natives/**/*.so
/natives/**/*.dll
/natives/**/*.o
/natives/src/main/native/*/lib-*/
/natives/src/main/native/*/build*/
# native libraries
/**/*.so
/**/*.dll
/**/*.o
98 changes: 98 additions & 0 deletions binary/src/main/java/net/daporkchop/lib/binary/oio/StreamUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Adapted from the Wizardry License
*
* Copyright (c) 2018-2020 DaPorkchop_ and contributors
*
* Permission is hereby granted to any persons and/or organizations using this software to copy, modify, merge, publish, and distribute it. Said persons and/or organizations are not allowed to use the software or any derivatives of the work for commercial use or any other means to generate income, nor are they allowed to claim this software as their own.
*
* The persons and/or organizations are also disallowed from sub-licensing and/or trademarking this software without explicit permission from DaPorkchop_.
*
* Any persons and/or organizations using this software must disclose their source code and have it publicly available, include this license, provide sufficient credit to the original authors of the project (IE: DaPorkchop_), as well as provide a link to the original project.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

package net.daporkchop.lib.binary.oio;

import lombok.NonNull;
import lombok.experimental.UtilityClass;
import net.daporkchop.lib.binary.stream.DataIn;
import net.daporkchop.lib.common.util.PorkUtil;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

/**
* Helper methods for dealing with OIO {@link InputStream}s and {@link OutputStream}s.
*
* @author DaPorkchop_
*/
@UtilityClass
public class StreamUtil {
/**
* Reads the entire contents of the given {@link InputStream} into a {@code byte[]}.
*
* @param in the {@link InputStream} to read
* @return the contents of the given {@link InputStream} as a {@code byte[]}
* @throws IOException if an IO exception occurs you dummy
*/
public byte[] toByteArray(@NonNull InputStream in) throws IOException {
if (in instanceof DataIn) {
//DataIn implementations might apply their own optimizations here
return ((DataIn) in).toByteArray();
} else {
byte[] arr = new byte[4096];
int pos = 0;
for (int i; (i = in.read(arr, pos, arr.length - pos)) != -1; pos += i) {
if (pos + i == arr.length) {
//grow array
byte[] old = arr;
System.arraycopy(old, 0, arr = new byte[arr.length << 1], 0, old.length);
}
}
return pos == arr.length ? arr : Arrays.copyOf(arr, pos); //don't copy if the size is exactly the size of the array already
}
}

/**
* Fills the given {@code byte[]} with data read from the given {@link InputStream}.
*
* @param in the {@link InputStream} to read from
* @param dst the {@code byte[]} to read to
* @return the {@link byte[]}
* @throws EOFException if the given {@link InputStream} reaches EOF before the given {@code byte[]} could be filled
* @throws IOException if an IO exception occurs you dummy
*/
public byte[] readFully(@NonNull InputStream in, @NonNull byte[] dst) throws EOFException, IOException {
return readFully(in, dst, 0, dst.length);
}

/**
* Fills the given region of the given {@code byte[]} with data read from the given {@link InputStream}.
*
* @param in the {@link InputStream} to read from
* @param dst the {@code byte[]} to read to
* @param start the first index (inclusive) in the {@code byte[]} to start writing to
* @param length the number of bytes to read into the {@code byte[]}
* @return the {@link byte[]}
* @throws EOFException if the given {@link InputStream} reaches EOF before the given number of bytes could be read
* @throws IOException if an IO exception occurs you dummy
*/
public byte[] readFully(@NonNull InputStream in, @NonNull byte[] dst, int start, int length) throws EOFException, IOException {
if (in instanceof DataIn) {
//DataIn implementations might apply their own optimizations here
((DataIn) in).readFully(dst, start, length);
} else {
PorkUtil.assertInRangeLen(dst.length, start, length);
for (int i; length > 0 && (i = in.read(dst, start, length)) != -1; start += i, length -= i) ;
if (length != 0) {
throw new EOFException();
}
}
return dst;
}
}
66 changes: 36 additions & 30 deletions binary/src/main/java/net/daporkchop/lib/binary/stream/DataIn.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from the Wizardry License
*
* Copyright (c) 2018-2019 DaPorkchop_ and contributors
* Copyright (c) 2018-2020 DaPorkchop_ and contributors
*
* Permission is hereby granted to any persons and/or organizations using this software to copy, modify, merge, publish, and distribute it. Said persons and/or organizations are not allowed to use the software or any derivatives of the work for commercial use or any other means to generate income, nor are they allowed to claim this software as their own.
*
Expand All @@ -20,16 +20,19 @@
import net.daporkchop.lib.binary.stream.netty.NettyByteBufIn;
import net.daporkchop.lib.binary.stream.nio.BufferIn;
import net.daporkchop.lib.binary.stream.stream.StreamIn;
import net.daporkchop.lib.common.util.PorkUtil;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -459,48 +462,51 @@ public CharSequence readText(long size, @NonNull Charset charset) throws IOExcep
}

/**
* Attempts to fill a byte array with data.
* <p>
* Functionally equivalent to:
* {@code return readFully(b, 0, b.length);}
* Fills the given {@code byte[]} with data.
*
* @param b the byte array to read into
* @return the {@code byte[]} that the data was read into
* @throws IOException if end of stream is reached before the required number required bytes are read
* @param dst the {@code byte[]} to read to
* @throws EOFException if EOF is reached before the given {@code byte[]} could be filled
* @throws IOException if an IO exception occurs you dummy
*/
public byte[] readFully(@NonNull byte[] b) throws IOException {
return this.readFully(b, 0, b.length);
public byte[] readFully(@NonNull byte[] dst) throws EOFException, IOException {
return this.readFully(dst, 0, dst.length);
}

/**
* Attempts to fill a given region of a byte array with data.
* Fills the given region of the given {@code byte[]} with data.
*
* @param b the byte array to read into
* @param off the offset in the array to write data to
* @param len the number of bytes to read
* @return the {@code byte[]} that the data was read into
* @throws IOException if end of stream is reached before the required number required bytes are read
* @param dst the {@code byte[]} to read to
* @param start the first index (inclusive) in the {@code byte[]} to start writing to
* @param length the number of bytes to read into the {@code byte[]}
* @return the {@code byte[]}
* @throws EOFException if EOF is reached before the given number of bytes could be read
* @throws IOException if an IO exception occurs you dummy
*/
public byte[] readFully(@NonNull byte[] b, int off, int len) throws IOException {
int i = 0;
while (len > 0 && (i = this.read(b, off + i, len)) != -1) {
len -= i;
}
if (i == -1) {
throw new IOException("Reached end of stream!");
public byte[] readFully(@NonNull byte[] dst, int start, int length) throws EOFException, IOException {
PorkUtil.assertInRangeLen(dst.length, start, length);
for (int i; length > 0 && (i = this.read(dst, start, length)) != -1; start += i, length -= i) ;
if (length != 0) {
throw new EOFException();
}
return b;
return dst;
}

/**
* Reads all available bytes from this stream, as returned by {@link #available()}.
* Reads the entire contents of this {@link DataIn} into a {@code byte[]}.
*
* @return all available bytes from this stream
* @return the contents of this {@link DataIn} as a {@code byte[]}
*/
public byte[] readAllAvailableBytes() throws IOException {
byte[] b = new byte[this.available()];
this.readFully(b);
return b;
public byte[] toByteArray() throws IOException {
byte[] arr = new byte[4096];
int pos = 0;
for (int i; (i = this.read(arr, pos, arr.length - pos)) != -1; pos += i) {
if (pos + i == arr.length) {
//grow array
byte[] old = arr;
System.arraycopy(old, 0, arr = new byte[arr.length << 1], 0, old.length);
}
}
return pos == arr.length ? arr : Arrays.copyOf(arr, pos); //don't copy if the size is exactly the size of the array already
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from the Wizardry License
*
* Copyright (c) 2018-2019 DaPorkchop_ and contributors
* Copyright (c) 2018-2020 DaPorkchop_ and contributors
*
* Permission is hereby granted to any persons and/or organizations using this software to copy, modify, merge, publish, and distribute it. Said persons and/or organizations are not allowed to use the software or any derivatives of the work for commercial use or any other means to generate income, nor are they allowed to claim this software as their own.
*
Expand All @@ -21,7 +21,9 @@
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import net.daporkchop.lib.binary.stream.DataIn;
import net.daporkchop.lib.common.util.PorkUtil;

import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.Charset;

Expand Down Expand Up @@ -177,22 +179,34 @@ public double readDoubleLE() throws IOException {

@Override
public CharSequence readText(long size, @NonNull Charset charset) throws IOException {
if (size > Integer.MAX_VALUE) {
if (size > Integer.MAX_VALUE) {
throw new IllegalArgumentException("size parameter too large!");
}
return this.buf.readCharSequence((int) size, charset);
}

@Override
public byte[] readFully(@NonNull byte[] b) throws IOException {
this.buf.readBytes(b);
return b;
public byte[] readFully(@NonNull byte[] dst) throws IOException {
this.buf.readBytes(dst);
return dst;
}

@Override
public byte[] readFully(@NonNull byte[] b, int off, int len) throws IOException {
this.buf.readBytes(b, off, len);
return b;
public byte[] readFully(@NonNull byte[] dst, int start, int length) throws EOFException, IOException {
PorkUtil.assertInRangeLen(dst.length, start, length);
if (this.buf.isReadable(length)) {
this.buf.readBytes(dst, start, length);
return dst;
} else {
throw new EOFException();
}
}

@Override
public byte[] toByteArray() throws IOException {
byte[] arr = new byte[this.buf.readableBytes()];
this.buf.readBytes(arr);
return arr;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from the Wizardry License
*
* Copyright (c) 2018-2019 DaPorkchop_ and contributors
* Copyright (c) 2018-2020 DaPorkchop_ and contributors
*
* Permission is hereby granted to any persons and/or organizations using this software to copy, modify, merge, publish, and distribute it. Said persons and/or organizations are not allowed to use the software or any derivatives of the work for commercial use or any other means to generate income, nor are they allowed to claim this software as their own.
*
Expand All @@ -19,7 +19,9 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import net.daporkchop.lib.binary.stream.DataIn;
import net.daporkchop.lib.common.util.PorkUtil;

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -51,9 +53,21 @@ public int read(@NonNull byte[] b, int off, int len) throws IOException {
}

@Override
public byte[] readFully(@NonNull byte[] b, int off, int len) throws IOException {
this.buffer.get(b, off, len);
return b;
public byte[] readFully(@NonNull byte[] dst, int start, int length) throws EOFException, IOException {
PorkUtil.assertInRangeLen(dst.length, start, length);
if (this.buffer.remaining() >= length) {
this.buffer.get(dst, start, length);
return dst;
} else {
throw new EOFException();
}
}

@Override
public byte[] toByteArray() throws IOException {
byte[] arr = new byte[this.buffer.remaining()];
this.buffer.get(arr);
return arr;
}

@Override
Expand All @@ -76,12 +90,12 @@ public long skip(long cnt) throws IOException {
}

@Override
public synchronized void mark(int readlimit) {
public void mark(int readlimit) {
this.buffer.mark();
}

@Override
public synchronized void reset() throws IOException {
public void reset() throws IOException {
this.buffer.reset();
}

Expand Down
5 changes: 0 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@ allprojects {
porklibVersion = "0.5.1-SNAPSHOT"

//dependency things
apacheCommonsCompressVersion = "1.16.1"
bouncycastleVersion = "1.59"
florianingerlRegexVersion = "1.1.9"
googleHttpClientVersion = "1.23.0"
gsonVersion = "2.8.5"
guavaVersion = "27.0-jre"
jnaVersion = "5.5.0"
junitVersion = "4.12"
lombokVersion = "1.16.20"
nettyVersion = "4.1.36.Final"
raknetVersion = "1.4.0"
sparseBitSetVersion = "1.1"
xzVersion = "1.8"
}

group "net.daporkchop"
Expand Down
Loading

0 comments on commit ada6412

Please sign in to comment.