mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 13:42:59 +00:00 
			
		
		
		
	Merge pull request #455 from Wilma456/fileread
Add read() to Filehandle
This commit is contained in:
		| @@ -6,6 +6,8 @@ import dan200.computercraft.api.lua.LuaException; | ||||
| import javax.annotation.Nonnull; | ||||
| import java.io.*; | ||||
|  | ||||
| import static dan200.computercraft.core.apis.ArgumentHelper.*; | ||||
|  | ||||
| public class EncodedInputHandle extends HandleGeneric | ||||
| { | ||||
|     private final BufferedReader m_reader; | ||||
| @@ -49,6 +51,7 @@ public class EncodedInputHandle extends HandleGeneric | ||||
|             "readLine", | ||||
|             "readAll", | ||||
|             "close", | ||||
|             "read", | ||||
|         }; | ||||
|     } | ||||
|  | ||||
| @@ -102,6 +105,26 @@ public class EncodedInputHandle extends HandleGeneric | ||||
|                 // close | ||||
|                 close(); | ||||
|                 return null; | ||||
|             case 3: | ||||
|                 // read | ||||
|                 checkOpen(); | ||||
|                 try | ||||
|                 { | ||||
|                     int count = optInt( args, 0, 1 ); | ||||
|                     if( count <= 0 || count >= 1024 * 16 ) | ||||
|                     { | ||||
|                         throw new LuaException( "Count out of range" ); | ||||
|                     } | ||||
|                     char[] bytes = new char[ count ]; | ||||
|                     count = m_reader.read( bytes ); | ||||
|                     if( count < 0 ) return null; | ||||
|                     String str = new String( bytes, 0, count ); | ||||
|                     return new Object[] { str }; | ||||
|                 } | ||||
|                 catch( IOException e ) | ||||
|                 { | ||||
|                     return null; | ||||
|                 } | ||||
|             default: | ||||
|                 return null; | ||||
|         } | ||||
|   | ||||
| @@ -94,6 +94,8 @@ function open( _sPath, _sMode ) | ||||
| 					return file.readLine() | ||||
| 				elseif sFormat == "*a" then | ||||
| 					return file.readAll() | ||||
|                 elseif _G.type( sFormat ) == "number" then | ||||
|                     return file.read( sFormat ) | ||||
| 				else | ||||
| 					error( "Unsupported format", 2 ) | ||||
| 				end | ||||
|   | ||||
| @@ -22,6 +22,7 @@ Functions on files opened with mode "r": | ||||
| readLine() | ||||
| readAll() | ||||
| close() | ||||
| read( number ) | ||||
|  | ||||
| Functions on files opened with mode "w" or "a": | ||||
| write( string ) | ||||
| @@ -36,4 +37,4 @@ close() | ||||
| Functions on files opened with mode "wb" or "ab": | ||||
| write( byte ) | ||||
| flush() | ||||
| close() | ||||
| close() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Daniel Ratcliffe
					Daniel Ratcliffe